i have a function to find the length of finding class, but it not working always return the length is ‘0’;
some thing is wrong with my code: can any one advice me the proper way. but i need to use the same structure of the code style to achieve this.
look on to my code :
<div id='list'>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<p></p>
</div>
function callMe(params){
$(params.list).click(function(){
$(this).addClass('c_on');
var length = $(params.list).find('.c_on').length;
$('p').text(length)// return always 0, even i clicked no.of list.
})
}
$(document).ready(function(){
var obj = {
list : $('ul').find('li')
}
callMe(obj);
})
style :
li.c_on{
border:1px solid green;
}
Here is the fiddle : http://jsfiddle.net/AeGvf/1/
You’re using
find:when you should be using
filter:Note that
findworks on the descendants whereasfilterworks on the elements themselves. You have a list of<li>s that don’t have any descendants sofindis searching an empty set. You should do this:Also, your
params.listis already a jQuery object so you don’t need to$(params.list), you can useparams.listdirectly:Demo: http://jsfiddle.net/ambiguous/uCGaY/