Reputation: 386
I have the following little snippet of code at the bottom of my page before the closing body tag:
var myAnchor = document.getElementById("tools").getElementsByTagName("a")[0];
var myHref = myAnchor.href;
myHref = "http://www.failblog.org";
alert(myHref);
The page alerts "http://www.failblog.org" as expected, but it doesn't actually change the value of the href attribute for the anchor. The link stubbornly retains its original href. Can anyone tell me what I'm doing wrong?
Upvotes: 0
Views: 116
Reputation: 2553
Change the code to this:
var myHref = "http://www.failblog.org";
var myAnchor = document.getElementById("tools").getElementsByTagName("a")[0];
myAnchor.href = myHref;
Upvotes: 0
Reputation: 10356
You did it the wrong way , try this:
myHref = "http://www.failblog.org";
var myAnchor = document.getElementById("tools").getElementsByTagName("a")[0];
myAnchor.href = myHref;
alert(myHref);
Upvotes: 2
Reputation: 854
When setting var myHref = myAnchor.href;
, the string value of myHref
will be set to the string value of myAnchor.href;
, the variable isn't assigned as a reference, but as a copy of the value.
Upvotes: 1