Reputation: 51611
I have a checkbox, how do I get its value using jquery? I have this example from searching:
var checked = $('input[type=checkbox]:checked').val() != undefined;
but how do I specify which checkbox I'm interested in, I want to only examine a checkbox with a particular id?
Thanks
Upvotes: 3
Views: 576
Reputation: 186562
var checked = $('input#foo:checked');
if ( checked.length && checked.val().length ) {
// do something with checked
}
Upvotes: 2
Reputation: 322492
You can use jQuery's is()
along with the :checked
selector:
var $theCheckbox = $('#theID');
if( $theCheckbox.is(':checked') ) {
var checked = $theCheckbox.val();
}
Upvotes: 2
Reputation: 10857
Let's say your id is #myid, then:
var checked = $('input[type="checkbox"]#myid').attr('checked');
if (checked) {
//box is checked
} else {
}
Upvotes: 1
Reputation: 25271
What do you mean by value? You can tell whether a box is checked with the checked
attribute. If it's checked, the value it sends to the server if the form submitted is in the value
attribute.
var mybox = $('input#myid');
if (mybox.length > 0 && mybox[0].checked) {
// do something, or use mybox[0].value
} else {
}
Upvotes: 1