Matej Posted June 3, 2014 Share Posted June 3, 2014 (edited) 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 June 3, 2014 by Matej Link to comment Share on other sites More sharing options...
dsonesuk Posted June 3, 2014 Share Posted June 3, 2014 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 More sharing options...
Matej Posted June 3, 2014 Author Share Posted June 3, 2014 (edited) it does the same thing when i add spacing and breaks also it shows #text via nodeName (on both of the cases) Edited June 3, 2014 by Matej Link to comment Share on other sites More sharing options...
dsonesuk Posted June 3, 2014 Share Posted June 3, 2014 <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 More sharing options...
Matej Posted June 3, 2014 Author Share Posted June 3, 2014 Thanks so basicly , every break counts as node here right? Link to comment Share on other sites More sharing options...
dsonesuk Posted June 3, 2014 Share Posted June 3, 2014 yep! but it is i think you will find it is browser dependant, just try it in different browsers to see if #text shows or other. Link to comment Share on other sites More sharing options...
Matej Posted June 3, 2014 Author Share Posted June 3, 2014 thanks! and what would be lastChild of <tr id="t1"><td>one</td></tr>? it shows "TD" but isnt it #text with nodeValue "one"? Link to comment Share on other sites More sharing options...
dsonesuk Posted June 3, 2014 Share Posted June 3, 2014 (edited) 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 June 4, 2014 by dsonesuk Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now