Press ESC to close

JavaScript IE anchor.text property undefined error issue

In Internet explorer, using Javascript one can’t access the anchor object’s “text” property. The work around for this is to use “innerHTML” property. However this works in all other browsers.

Complete description with code snippet is shown below

HTML DOM Anchor object has a “text” property that gives the text between opening and closing anchor tags for ex.

<a href=”http://www.google.com/&#8221; id=”lnkId” &gt; Show Google &lt;/a>

This property words well in all browsers except IE. On IE you get a Java Script error “undefined” on accessing this property.
The solution to this is shown in code snippet below. This script will look for an anchor with id “lnkId” and will show an alert window with the Text between the opening and closing tags

<script language=”javascript” type=”text/javascript”>

var anchorObj = document.getElementById(“lnkId”);

// For all browsers except IE
alert(anchorObj.text);

// For IE
alert(anchorObj.innerHTML);

</script>

Leave a Reply

%d bloggers like this: