Checkbox selector
The selector :checkbox
selects checkboxes. See the tag checkbox
. The equivalent of $(':checkbox')
is $('[type=checkbox]')
. Since :checkbox
is not part of the CSS specification, for better performance in modern browsers it is better to use [type='checkbox']
instead.
Syntax
This is how we select checkboxes:
$(':checkbox');
As with other pseudo-class selectors (those starting with ':'
), it is better to precede ':'
with the name of the tag or another selector, otherwise the selector '*'
will be applied, i.e. $(':checkbox')
will be treated as $('*:checkbox')
, so it is better to use $('input:checkbox')
instead.
Example
Let's select all the checkboxes, wrap them in a span. Then give the spans a green background and a red border using the css
method:
<form>
<input type="button" value="button">
<input type="file">
<input type="checkbox">
<button>button</button>
<input type="reset">
<input type="radio" name="test">
<input type="radio" name="test">
<input type="checkbox">
<input type="text">
</form>
$('form input:checkbox')
.wrap('<span></span>')
.parent()
.css({background: 'green', border: '2px red solid'});
$('form').submit(function(event) {
event.preventDefault(); // prevents form submission
});