jQuery, dynamically count number of elements on a page
I am trying to count the number of inputs on a page with a particular class.
$(".count").click开发者_运维问答(function(){
var named = $(this).parent().find("input").eq(1).attr('class');
var count = $('input[name='+named+']').length;
alert(count + ' of class ' + named);
});
The count always returns a value of zero. Have I set the 'count' variable correctly? If not, how should I do this.
thanks.
If you want them by class, you'd need to change this:
$('input[name='+named+']')
to this:
$('input[class='+named+']')
or this:
$('input.'+named)
If the element you get in the first line of the handler has more than one class, you'll need to change it from this:
var named = $(this).parent().find("input").eq(1).attr('class');
to this:
var named = $(this).parent().find("input").eq(1).attr('class').split(/\s+/).join('.');
so that you end up with:
someClass.anotherClass
Then use this one:
$('input.'+named)
精彩评论