I would like to select a set of elements that are both of a certain input type (say, a checkbox) and have a certain class using jQuery. However, when I try the following:
$("input:checkbox .myClass")
I don't get any items returned. How can I accomplish this in jQuery?
This question is related to
jquery
jquery-selectbox
You have to use (for checkboxes) :checkbox
and the .name
attribute to select by class.
For example:
$("input.aclass:checkbox")
The :checkbox
selector:
Matches all input elements of type checkbox. Using this psuedo-selector like
$(':checkbox')
is equivalent to$('*:checkbox')
which is a slow selector. It's recommended to do$('input:checkbox')
.
You should read jQuery documentation to know about selectors.
You should use the class name like this
$(document).ready(function(){
$('input.addCheck').prop('checked',true);
});
Try Using this a live demo
Try this:
$("input:checkbox").hasClass("myClass");
Just in case any dummies like me tried the suggestions here with a button and found nothing worked, you probably want this:
$(':button.myclass')
If you want to get the inputs of that type with that class use:
$("input.myClass[type=checkbox]")
the [] selector syntax allows you to check against any of the elements attributes. Check out the spec for more details
Source: Stackoverflow.com