I try to count the number of children inside a div using jquery $(this) selector and the element’s class. And the results are different. I thought jquery’s $(this) refers to the owner object of function, is there any thing special about $(this) that I am missing?
$('.parent').ready(function(){
$('.parent').children().length; // 6
$(this).children().length; // 1
});
This:
is the correct way to do it. This:
is a syntax error. If you really wanted to iterate through the children you could use “.each()” but you’d have to do it properly:
Note that inside the “.each()” callback,
thiswill refer to each child in succession as the function is called by jQuery.