freeBSD
freeBSD

Reputation: 317

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.

Upvotes: 3

Views: 3146

Answers (4)

VBH
VBH

Reputation: 9

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

Check This

            $(".PagingLink").each( function(index,element) {
            $(element).attr('href').replace(/STRINGID/gi,"0000123450");                                                              
    });

.attr("value").replace('0', 'Day')

Upvotes: 0

Skatox
Skatox

Reputation: 4284

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.

Upvotes: 4

Gromer
Gromer

Reputation: 9931

You need to change the text in the onclick attribute, not the href attribute. See this Fiddle: http://jsfiddle.net/fQEdV/2/

Changing the href won't dictate where the page will go since Sharepoint is using javascript to redirect the user in the click event for the link. If the user has javascript disabled, your script to update this url won't run for them anyways.

Upvotes: 0

andyb
andyb

Reputation: 43823

You are probably getting a JavaScript error with your current jQuery code, since there is no method called onclick(). You can access the onclick value by using the attr() method like so:

url = $(element).attr('onclick');

The rest of your code should execute correctly.

Upvotes: 1

Related Questions