Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if a checkbox is checked with jQuery

If the checkbox is checked, then I only need to get the value as 1; otherwise, I need to get it as 0. How do I do this using jQuery?

$("#ans").val() will always give me one right in this case:

<input type="checkbox" id="ans" value="1" /> 
like image 925
Rajeev Avatar asked Jan 27 '11 05:01

Rajeev


People also ask

How do you check if a checkbox is checked?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.

Which method of checkbox view is used to check if the checkbox option is ticked?

Checking if a checkbox is checked by using is The is method of jQuery provides us a way to match one or multiple elements against a given selector, jQuery object, or element. It returns true if at least one element matches.

How do you check if a checkbox is checked react?

Use the target. checked property on the event object to check if a checkbox is checked in React, e.g. if (event. target. checked) {} .

How do you check if all the checkboxes are checked in Javascript?

type == 'checkbox' && inputs[x]. name == 'us') { is_checked = inputs[x]. checked; if(is_checked) break; } } // is_checked will be boolean 'true' if any are checked at this point.


1 Answers

Use .is(':checked') to determine whether or not it's checked, and then set your value accordingly.

More information here.

like image 76
Andy Mikula Avatar answered Sep 28 '22 06:09

Andy Mikula