DOM .lastChild

Posted in:beginner by admin on 2008-04-13
Social Bookmarking
Add to: Yigg Add to: Digg Add to: Del.icio.us Add to: Reddit Add to: Jumptags Add to: Upchuckr Add to: Simpy Add to: StumbleUpon Add to: Slashdot Add to: Netscape Add to: Furl Add to: Yahoo Add to: Spurl Add to: Google Add to: Blinklist Add to: Blogmarks Add to: Diigo Add to: Technorati Add to: Newsvine Add to: Blinkbits Add to: Ma.Gnolia Add to: Smarking Add to: Netvouz

So have you read any other DOM method tutorials yet ? If not thats ok.

In this tutorial we are talking about .lastChild

So if you have read the .parentNode and/or .firstChild tutorial already. its roughly the same! You can pretty much guess what it does. It grabs the last child(element) of the div or table tag.

Lets look at an example:

<div id=”parent”>
<div >first</div>
<div >second</div>
<div >third</div>
<div >fourth</div>
</div>

so what we have here is some div tags. 1 parent Div and 4 child divs.

so here is how we could use the .lastChild DOM element.

document.getElementByID(“parent”);

first you want to call the ID ( I am using an ID for easy understanding. you won’t always have ID’s to work with.) Once you have that ID called in a variable or what not. you can use .lastChild

document.getElementByID(“parent”).lastChild;

What that just did was call the Last (Child) Div tag.

<div >fourth</div>

that one :P

from there you can use innerHTML or use more DOM if there are more elements inside that tag.

Look in the Tutorials for more DOM methods!

Back