how can i count elements with the class item which is contained in a variable
so far http://jsfiddle.net/jGj4B/
var post = '<div/><div class="item"></div><div class="item"></div><div class="item"></div><div class="item"></div>';
alert($(post).find('.item').length); // i have tried .size() in place of .length
You want
filterinstead offind:findlooks for descendent element, and there are none.If you want both top-level items and descendants, you could do this:
Though wrapping it in a span as suggested in another answer is probably much more understandable.
EDIT 7/19/2013:
andSelfhas been deprecated as of v1.8 andaddBackshould be used in its place.