1

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.

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

oh man, silly. yes, that is it. thank you. I think it is time for a break.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.