JulianJohannesen
JulianJohannesen

Reputation: 386

href property refuses to be re-set after page load

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

Answers (3)

Virendra
Virendra

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

Ofir Baruch
Ofir Baruch

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

JimmyMcHoover
JimmyMcHoover

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

Related Questions