Inside JavaScript
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
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.
|