faqts : Computers : Programming : Languages : JavaScript : Links

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

17 of 19 people (89%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I get the text of the clicked link?

Apr 3rd, 2000 09:35
Martin Honnen, Laurent LESTRADE,


With NN3/4:
  document.links[linkIndex].text
With IE4+:
  document.links[linkIndex].innerText
With NN6 you need to collect the text or just access the first text 
node:
  document.links[linkIndex].firstChild.nodeValue

Here is an example showing the text in a confirm dialog:

<HTML>
<HEAD>
<SCRIPT>
function getLinkText (link) {
  return link.text ? link.text :
         link.innerText ? link.innerText :
         link.firstChild.nodeType == 3 ? link.firstChild.nodeValue :
         '';
}
</SCRIPT>
</HEAD>
<BODY>
<A HREF="http://JavaScript.FAQTs.com"
   ONCLICK="var t = getLinkText(this);
            return confirm('Goto ' + t);"
>
JavaScript.FAQTs
</A>
|
<A HREF="http://www.kibo.com"
   ONCLICK="var t = getLinkText(this);
            return confirm('Goto ' + t);"
>
GOD
</A>
</BODY>
</HTML>