lastChild property
Returns a reference to the last 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 last child.
Default: this property has no default value.
Example HTML code 1:
This example illustrates the use of the lastChild property and childNodes collection:
|
||||
<head> <script type="text/javascript"> function GetLastValue () { var selectObj = document.getElementById ("mySelect"); alert (selectObj.lastChild.text); // other way to get the last child alert (selectObj.childNodes[childNodes.length - 1].text); } </script> </head> <body> <button onclick="GetLastValue ();">Get the value of the last child!</button> <select id="mySelect" multiple="multiple"> <option value="0">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