[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cross-Browser anchor nodeValue access

Posted on 2006-05-30
5
Medium Priority
?
341 Views
Last Modified: 2012-08-13

I am having trouble accessing the anchor tag's text with DOM methods if FF.  Why would this not work? Is this a documented incompatibility?  who is right? IE or FF?  

Please check the script below (not the HTML) and see if you can fix it using DOM methods only in IE and FF.


This works in IE but not FF:

<body>

<div id="test">
  <a href="#test">test</a>
</div>

</body>

<script>
alert( document.getElementById("test").firstChild.firstChild.nodeValue);
</script>

</html>
0
Comment
Question by:0h4crying0utloud
  • 3
  • 2
5 Comments
 
LVL 8

Accepted Solution

by:
KennyTM earned 2000 total points
ID: 16795306
In Firefox the empty spaces between the <div> and the <a> is counted as a text node too, so .firstChild here actually refers to the spaces there, and that's why your script doesn't work.
0
 
LVL 8

Expert Comment

by:KennyTM
ID: 16795343
If you want to refer specifically the <a> tag inside the <div> you can get the list by .getElementsByTagName(), e.g.,

document.getElementById("test").getElementsByTagName("a")[0].firstChild.nodeValue
0
 
LVL 10

Author Comment

by:0h4crying0utloud
ID: 16795354


Thanks so much. That works perfectly.  Now is IE suppose to count the whitespace as a separate textNode?
0
 
LVL 8

Expert Comment

by:KennyTM
ID: 16795382
>> Now is IE suppose to count the whitespace as a separate textNode?

No. IE will ignore empty ("all whitespace") text nodes.
0
 
LVL 10

Author Comment

by:0h4crying0utloud
ID: 16795402

OK, thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question