The firstChild and lastChild Properties


The firstChild and lastChild Properties

The firstChild and lastChild properties return the first and last child nodes of a specific node, or a value of null if there is no such child. You can see the support for these properties in Table 5.22.

Table 5.22. The firstChild and lastChild Properties

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

firstChild

     

x

     

x

x

x

 

Read-only

 

Type: Node object

lastChild

     

x

     

x

x

x

 

Read-only

 

Type: Node object

Here's an example putting the firstChild and lastChild properties to workin this case, the code will report the names of the two children of a form using these properties:

(Listing 05-13.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Using the firstChild and lastChild Properties          </TITLE>      </HEAD>      <BODY>          <H1>Using the firstChild and lastChild Properties</H1>          <FORM NAME="form1">              <INPUT TYPE="BUTTON" NAME="button1" VALUE="Click Me!" ONCLICK="getText()">              <INPUT TYPE="TEXT" NAME="text1"></FORM>          <SCRIPT LANGUAGE="JavaScript">              <!--  document.write("document.all.form1.firstChild.name = "   + document.all.form1.firstChild.name + "<BR>")   document.write("document.all.form1.lastChild.name = "   + document.all.form1.lastChild.name + "<BR>")  // -->          </SCRIPT>      </BODY>  </HTML> 

You can see the results in Figure 5.10.

Figure 5.10. Using the firstChild and lastChild properties.

graphics/05fig10.gif



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