2

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 How to change the href for a hyperlink using jQuery but it's not working for me any help is appreciated.

Community
  • 1
  • 1
Developer
  • 2,987
  • 10
  • 40
  • 51

4 Answers4

6

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
  • 377,238
  • 77
  • 533
  • 578
  • 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. Jul 23 '10 at 19:37
3

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
  • 31,987
  • 28
  • 102
  • 155
1

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
  • 101,701
  • 37
  • 181
  • 258
  • these anchors are generated by programming. there are so many other anchors I couldn't give all of them the same id – Developer Jul 23 '10 at 19:34
0

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
  • 13,915
  • 6
  • 44
  • 73