Jump to content

firstchild


Matej

Recommended Posts

Hello gentlemen

<table id="outside">    <tr><td id="t1">one</td></tr>    <tr><td id="t2">two</td></tr></table>
var t2 = document.getElementById("t2");alert(t2.firstChild.nodeValue)

Why does this alert "two" (value of t2) while t2 does not have childrens?

 

but when i use

var t2 = document.getElementById("outside");alert(t2.firstChild.nodeValue)

it does not alert anything

Also can someone explain to me when do i use "true" and when do i use "false" in element.addEventListener()?

 

 

Thanks for answering and have a nice day

Edited by Matej
Link to comment
Share on other sites

Because

<tr><td id="t2">two</td></tr>

 

has no line breaks or spacing which would register as text node, it shows 'two', where as

 

<table id="outside"> <tr><td id="t1">one</td></tr> <tr><td id="t2">two</td></tr></table>

 

does have line breaks and probably space it would register that as a node and show it as a blank, change '.nodeValue' to '.nodeName' and see what happens, also JavaScript created tables, generally create tbody elements etc, by default so you may see these and not what you expected.

Link to comment
Share on other sites

        <table id="outside"><!-- line break or space = nodename #text and show as blank with nodevalue (may show as tbody)-->            <tr><td id="t1">one</td></tr> <!--No line break or space but text of 'one' = nodename #text and show as 'one' with nodevalue -->            <tr>                <td id="t2"><!-- line break or space = nodename #text and show as blank with nodevalue -->                    two                </td>            </tr>        </table>
Link to comment
Share on other sites

No, 'one' is first and last child of <td>, as the td is the first and last child of <tr> with id ref of 't1'

 

To show nodeValue 'one' it would have to be

 

alert(t1.childNodes[0].lastChild.nodevalue); ='one'

alert(t1.childNodes[0].lastChild.nodeName); = '#text'

 

tr -> td -> text

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...