tags:

views:

55

answers:

4

A site I am working on has links for add to cart I would like to change that link to point to different page how can I achieve this in jquery.

$(document).ready(function() {
    //alert('Welcome to StarTrackr! Now no longer under police …');
    $("a[href='http://www.somesite.com/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,<web::ISSUE>,npus,$99.00,npcdn,$99.00']").attr('href', 'http://www.live.com/');
});

I am trying this got this idea from here http://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery but it's not working for me any help is appreciated.

+1  A: 

The code you posted looks right (although, I would give the <a> tag a ID attribute, so that you could avoid specifying the long search string.

<a id="MyLink" href='/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,<web::ISSUE>,npus,$99.00,npcdn,$99.00'> text text text </a>

<script>
$(document).ready(function() 
{ 
    $("#MyLink").attr('href', 'http://www.live.com/'); 
}); 
</script>
James Curran
these anchors are generated by programming. there are so many other anchors I couldn't give all of them the same id
fzshah76
+4  A: 

Use a selector with *:

$("a[href*='scAddItem']").attr('href', 'http://www.live.com/');

The links' href attribute will be changed for any link that contains scAddItem somewhere in its url. You can modify it for your exact string though.

More Readings:

Sarfraz
This is a good answer. I'm guessing the OPs problem is the selector is too specific and isn't returning the link. This should work IMO.
KP
Wonderfull that did it Thanks
fzshah76
@fzshah76: You are welcome...
Sarfraz
+2  A: 

i'd suggest adding an id to that link so you can reference it directly, much faster and simpler than trying to match on its href:

<a id="cartLink" href="/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,,npus,$99.00,npcdn,$99.00">Add To Cart</a>

<script type="text/javascript">
  $("#cartLink").attr('href','http://www.live.com');
</script>
brad
A: 

Try this:

$("a[href='/scAddItem.aspx?action=add&BJID=421&extra=type,journalIssue,volume,2,issue,,npus,$99.00,npcdn,$99.00']").click(function() {
    $(this).attr('href', 'http://www.live.com/');
});

Or this if you could change your html.

$("a.mylink").click(function() {
    $(this).attr('href', 'http://www.live.com/');
});
gearsdigital