Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

How do I get the href attribute of a HTML tag from Javascript

12:40 am on Dec 28, 2002 (gmt 0)

New User

10+ Year Member

joined:Dec 20, 2002
votes: 0

I need to be able to get the HREF attribute of a link tag from javascript.

Heres the link:

<a href="mockup2.htm" id="corporateSubMenuOneActuator" class="corporatesubmenuoneactuator">Overview</a>

And the javascript that isnt working for me:

if (actuatorType == "linkActuator")
window.location = menu.url;


How do I get the value of the links HREF?

Derek Basch

6:32 pm on Dec 28, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member korkus2000 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 20, 2002
votes: 0

Your going to need to use the links array.


where i is the number of the link in the array.

document.links[1].href="url"; // = mylink.com from below if those are the only links on the page or the first three.

<a href="yourlink.com">test1</a>
<a href="mylink.com">test1</a>
<a href="theirlink.com">test1</a>

Try something like this

function locateHref(arraynum){
var arraynum;
var whatHref;
whatHref = document.links[arraynum].href;

<a href="yourlink.com" onclick="locateHref(0)">test1</a>
<a href="mylink.com" onclick="locateHref(1)">test1</a>
<a href="theirlink.com" onclick="locateHref(2)">test1</a>