I know using <td>
outside <table>
is an invalid markup but it is still a node in HTML DOM. That is why I am able to see those two cells rendered. jQuery
is unable to get me those cells. Here is the JSBin Demo and Code
HTML
<table>
<tr><td class='cell1'>Cell inside table</td></tr>
</table>
<tr>
<td class='cell2'>Cells without table</td>
<td class='cell2'>Cells without table</td>
</tr>
JS
var $a = $('#a');
var numCells1 = $('.cell1').length;
var numCells2 = $('.cell2').length;
$a.html('Num Cell1:' + numCells1 + ' Num Cell2:' + numCells2);
Output
Num Cell1:1 Num Cell2:0
Why Num Cell2 is 0? What am I doing wrong ?