homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

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

10+ Year Member

Msg#: 249 posted 12:40 am on Dec 28, 2002 (gmt 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



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

Msg#: 249 posted 6:32 pm on Dec 28, 2002 (gmt 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>

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved