Search code examples
jquerysharepointattributesonclickhref

Modifying an onclick URL parameter of a hyperlink with jQuery


I have a series of links on a SharePoint page and am trying to modify one of the common URL parameters of them all. The links all look similar to this:

<a href="javascript:" onclick="javascript:RefreshPageTo(event, '/dev/Pages/stPCT.aspx?Paged=TRUE&amp;p_ID=357\u0026PageFirstRow=31\u0026FilterField1=Number&amp;FilterValue1=0000123450&amp;&amp;\u0026View={FC071FA0-12AA-7854-905E-0C4429FFFD52}&amp;thisID=STRINGID');javascript:return false;" id="PagingLink" class="PagingLink"> Next</a>

What I am trying to get to happen is that the link is rewritten to change "thisID=STRINGID" to "thisID=0000123450"... So far I have tried (without success) the following.

$(".PagingLink").each( function(index,element) {
    url = $(element).onclick();
    console.log(url);
    newurl = url.replace(/STRINGID/gi,"0000123450");
    $(element).attr('href', newurl);                                                               
});

Any and all help would be greatly appreciated.


Solution

  • I'll do it like this:

     $(".PagingLink").each( function(index,element) {
            url = $(element).attr('onclick');
            console.log(url);
            newurl = url.replace(/STRINGID/gi,"0000123450");
            $(element).attr('href',newurl);                                                               
        });
    

    Your mistake was in the .onclick, you should get it with attr() because it garantess you to get the correct value.