0

I'm trying to change the content of a href using jQuery. I've found a number of good answers on this site, and am trying to use this one to implement it:

How to change the href for a hyperlink using jQuery

I have it wrapped in an if statement like this:

if(window.location.href == "http://test.com/Home.aspx"){

    $("a[href='https://www.surveymonkey.com/s/tester123']").attr('href', 'https://www.surveymonkey.com/s/tester234');
}

I've put some console logs in place and they show that this line of code is being reached, and that this:

$("a[href='https://www.surveymonkey.com/s/tester123']")

Is returning the correct link. However the href value is never replaced when I check the page source.

I'm using http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js?build=7111092

I'm a bit stuck on how to debug what the issue is, can anyone help spot it? Hopefully its just a stupid mistake!

--EDIT--

I have now also checked it using inspect element and it's not been replaced unfortunately.

Also thanks to @Vohuman and @roxxypoxxy for the info on not being able to see the change unless I hovered over it. Stupidly I was expecting it to work like a find/replace!

Community
  • 1
  • 1
hlh3406
  • 1,382
  • 5
  • 29
  • 46
  • 4
    "However the href value is never replaced when I check the page source.". You should inspect the element and see the live representation of the DOM elements. – Ram Apr 22 '15 at 15:02
  • The page source is the page source, you cannot change it (fortunately) client side... – A. Wolff Apr 22 '15 at 15:05
  • I've just checked it by inspecting the element and it's still the same unfortunately. – hlh3406 Apr 22 '15 at 15:05
  • @hollie3406 Then please provide minimalistic sample to replicate issue – A. Wolff Apr 22 '15 at 15:06
  • 1
    The page source can never be changed by the DOM manipulation done by the javascript. To validate your href replacement is working, you can hover over your link and see in the status bar the link it is pointing. – roxxypoxxy Apr 22 '15 at 15:07
  • @A.Wolff what do you mean sorry? – hlh3406 Apr 22 '15 at 15:07
  • @hollie3406 Provide enough code in question itself to replicate your issue – A. Wolff Apr 22 '15 at 15:08
  • 2
    here's a jsfiddle for this https://jsfiddle.net/3vaqn2qt/1/ I added the same version of jquery provided under external resources and it works correctly you can hover on link to shows it display href ends with `tester234 ` – Ihab Khattab Apr 22 '15 at 15:20
  • You could have also simply done `$("a[href='https://www.surveymonkey.com/s/tester123']")[0].href = 'https://www.surveymonkey.com/s/tester234';` – lshettyl Apr 22 '15 at 15:30

1 Answers1

3

I think it is a Jquery version issue. You are using Jquery 1.5. In Jquery docs they have mentioned this

Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.

Source https://api.jquery.com/attr/

So try to use higher JQuery version or use .prop() instead of .attr()