Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery `children` versus ` find`

Tags:

HTML

<table id='t'>
  <tr>
    <td id='foo' class='a b c'>blah</td>
    <td id='bar' class='a c'>bloo</td>
    <td id='zip' class='a b c'>blop</td>
 </tr>
</table>

Using jQuery, why does the following children call return 0

$('#t').children('tr').length

but find returns 1?

$('#t').find('tr').length

https://jsfiddle.net/o71x6co6/1/

like image 613
Kevin Meredith Avatar asked Oct 21 '16 15:10

Kevin Meredith


1 Answers

Because browsers automatically add a tbody element if you don't.

console.log($('#t').children().get(0).tagName);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id='t'>
  <tr>
    <td id='foo' class='a b c'>blah</td>
    <td id='bar' class='a c'>bloo</td>
    <td id='zip' class='a b c'>blop</td>
 </tr>
</table>
like image 125
Heretic Monkey Avatar answered Oct 11 '22 14:10

Heretic Monkey