firstChild property
Returns a reference to the first child of the current element.
The childNodes collection contains the direct descendants of an element, in source order.
The firstChild property retrieves the first item from the childNodes collection.
Similarly, the lastChild property returns the last item from the childNodes collection.
The childNodes collection also contains the text and comment nodes, not only HTML elements.
If you only need those HTML elements that are direct descendants of an element, use the children collection.
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read-only.
Possible values:
Reference to the first child or null if it does not exist.
Default: this property has no default value.
Example HTML code 1:
This example illustrates the use of the firstChild property and childNodes collection:
|
||||
<head> <script type="text/javascript"> function GetFirstText () { var selectObj = document.getElementById ("mySelect"); alert (selectObj.firstChild.text); // other way to get the first child alert (selectObj.childNodes[0].text); } </script> </head> <body> <button onclick="GetFirstText ();">Get the text of the first child!</button> <select id="mySelect" multiple="multiple"><option>Apple</option> <option>Peach</option> <option>Pear</option> </select> </body> |
||||
|
||||
Did you find this example helpful?
|
Supported by objects:
attribute, document, DocumentFragment, XMLDocument
HTML elements:
a, abbr, acronym, address, applet, b, bdo, big, blink, blockquote, body, button, caption, center, cite, code, col, colgroup, dd, del, dfn, dir, div, dl, dt, em, fieldset, font, form, frameset, h1, h2, h3, h4, h5, h6, head, html, i, img, ins, kbd, keygen, label, legend, li, listing, map, marquee, menu, nobr, noframes, noscript, object, ol, optgroup, option, p, plaintext, pre, q, rt, ruby, s, samp, select, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, u, ul, var, xml, xmp
Related pages:
External links:
User Contributed Comments