The childNodes Property


The childNodes Property

The childNodes property is one we've already seen in Chapter 4, "Handling the Browser Environment"this property returns an array of the child nodes of the current node. You can see the support for this property in Table 5.11.

Table 5.11. The childNodes Property

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

childNodes

     

x

   

x

x

x

x

 

Read-only

 

Type: Node array

In the following example, I'm using the childNodes property to read the text in a text field:

(Listing 04-12.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Accessing HTML Elements          </TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             function getText()              {  if(navigator.appName == "Netscape") {   var body1 = document.getElementById("body"   var h1 = body1.childNodes[1]   var form1 = h1.nextSibling.nextSibling   var text1 = form1.childNodes[1]   alert(text1.value)   }   if (navigator.appName == "Microsoft Internet Explorer") {   var body1 = document.getElementById("body")   var h1 = body1.childNodes[0]   var form1 = h1.nextSibling   var text1 = form1.childNodes[0]   alert(text1.value)   }  }              // -->          </SCRIPT>      </HEAD>      <BODY ID="body">          <H1>Accessing HTML Elements</H1>          <FORM NAME="form1">              <INPUT TYPE="TEXT" NAME="text1" ID="text1">               <INPUT TYPE="BUTTON" VALUE="Click Me!" ONCLICK="getText()">          </FORM>      </BODY>  </HTML> 

You can see this code at work in Figure 4.10.

Tip

If there are no child nodes, the length property of the childNodes array will be 0.




Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net