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,879
  • 10
  • 38
  • 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
  • 367,681
  • 72
  • 526
  • 573
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,109
  • 28
  • 99
  • 152
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
  • 98,636
  • 35
  • 176
  • 255
  • 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,340
  • 6
  • 42
  • 72