Thursday, June 6, 2024
 Popular · Latest · Hot · Upcoming
87
rated 0 times [  92] [ 5]  / answers: 1 / hits: 18395  / 12 Years ago, sat, march 24, 2012, 12:00:00

Why does the following return 0?



 <p id=g>
<div>kk</div>
<div>ee</div>
<div>jff</div>
</p>


<script type=text/javascript>
var ii = document.getElementById(g);
var hh = ii.getElementsByTagName('div');
document.write(hh.length);
</script>

More From » dom

 Answers
4

Because you can't have a <div> in a <p>. Paragraphs can only have inline elements as children.



As soon as the parser encounters a <div>, it auto-closes the <p>.



Compare



<p id=g>
<span>kk</span>
<div>ee</div>
<div>jff</div>
</p>

<script type=text/javascript>
var ii = document.getElementById(g);
var hh = ii.getElementsByTagName('span');
alert(hh.length);
</script>​

[#86632] Thursday, March 22, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
scarlett

Total Points: 491
Total Questions: 94
Total Answers: 100

Location: New Caledonia
Member since Thu, Mar 23, 2023
1 Year ago
;