Finding the Value of a Node

Finding the text contained within an element is actually trickier than it sounds. For example, the variable swim has a DOM node that contains the following HTML:


<p class="swim">Swim</p>  

It clearly contains the text "swim", but this is held in a text node, which is the first child of the swim node:


swimTextNode = swim.firstChild;
<< #text "Swim
  

Now that we have a reference to the text node, we can find the text contained inside it using the nodeValue method:


swimTextNode.nodeValue;
<< "Swim"
  

A quicker way of doing this is to use the textContent property. This will return the text content of an element as a string: 


swim.textContent
<< "Swim"
  

Note that Internet Explorer version 8 does not support the textContent property, but has the innerText property, which works in a similar way.  

0 Response to "Finding the Value of a Node"

Post a Comment