Testing if a checkbox is checked with jQuery

JavascriptJqueryCheckboxJquery Selectors

Javascript Problem Overview


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" />

Javascript Solutions


Solution 1 - Javascript

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

More information here.

Solution 2 - Javascript

$("#ans").attr('checked') 

will tell you if it's checked. You can also use a second parameter true/false to check/uncheck the checkbox.

$("#ans").attr('checked', true);

Per comment, use prop instead of attr when available. E.g:

$("#ans").prop('checked')

Solution 3 - Javascript

Just use $(selector).is(':checked')

It returns a boolean value.

Solution 4 - Javascript

// use ternary operators
$("#ans").is(':checked') ? 1 : 0;

Solution 5 - Javascript

@StefanBrinkmann's answer is excellent, but incomplete for beginners (omits the variable assignment). Just to clarify:

// this structure is called a ternary operator
var cbAns = ( $("#ans").is(':checked') ) ? 1 : 0;

It works like this:

 var myVar = ( if test goes here ) ? 'ans if yes' : 'ans if no' ;

Example:

var myMath = ( 1 > 2 ) ? 'yes' : 'no' ;
alert( myMath );

Alerts 'no'

Solution 6 - Javascript

You can try this:

$('#studentTypeCheck').is(":checked");

Solution 7 - Javascript

I've found the same problem before, hope this solution can help you. first, add a custom attribute to your checkboxes:

<input type="checkbox" id="ans" value="1" data-unchecked="0" />

write a jQuery extension to get value:

$.fn.realVal = function(){
    var $obj = $(this);
    var val = $obj.val();
    var type = $obj.attr('type');
    if (type && type==='checkbox') {
        var un_val = $obj.attr('data-unchecked');
        if (typeof un_val==='undefined') un_val = '';
        return $obj.prop('checked') ? val : un_val;
    } else {
        return val;
    }
};

use code to get check-box value:

$('#ans').realVal();

you can test here

Solution 8 - Javascript

$('input:checkbox:checked').val();        // get the value from a checked checkbox

Solution 9 - Javascript

<input type="checkbox" id="ans" value="1" />

Jquery : var test= $("#ans").is(':checked') and it return true or false.

In your function:

$test =($request->get ( 'test' )== "true")? '1' : '0';

Solution 10 - Javascript

You can also use:

$("#ans:checked").length == 1;

Solution 11 - Javascript

Use:

$("#ans option:selected").val()

Solution 12 - Javascript

function chkb(bool){
if(bool)
return 1;
return 0;
}

var statusNum=chkb($("#ans").is(':checked'));

statusNum will equal 1 if the checkbox is checked, and 0 if it is not.

EDIT: You could add the DOM to the function as well.

function chkb(el){
if(el.is(':checked'))
return 1;
return 0;
}

var statusNum=chkb($("#ans"));

Solution 13 - Javascript

I've came through a case recently where I've needed check value of checkbox when user clicked on button. The only proper way to do so is to use prop() attribute.

var ansValue = $("#ans").prop('checked') ? $("#ans").val() : 0;

this worked in my case maybe someone will need it.

When I've tried .attr(':checked') it returned checked but I wanted boolean value and .val() returned value of attribute value.

Solution 14 - Javascript

There are Several options are there like....

 1. $("#ans").is(':checked') 
 2. $("#ans:checked")
 3. $('input:checkbox:checked'); 

If all these option return true then you can set value accourdingly.

Solution 15 - Javascript

Try this

$('input:checkbox:checked').click(function(){
    var val=(this).val(); // it will get value from checked checkbox;
})

Here flag is true if checked otherwise false

var flag=$('#ans').attr('checked');

Again this will make cheked

$('#ans').attr('checked',true);

Solution 16 - Javascript

 $("#id").prop('checked') === true ? 1 : 0;

Solution 17 - Javascript

You can get value (true/false) by these two method

$("input[type='checkbox']").prop("checked");
$("input[type='checkbox']").is(":checked");

Solution 18 - Javascript

If you want integer value of checked or not, try:

$("#ans:checked").length

Solution 19 - Javascript

First check the value is checked

$("#ans").find("checkbox").each(function(){
    if ($(this).prop('checked')==true){ 
    var id = $(this).val()
    }
});

Else set the 0 value

Solution 20 - Javascript

You can perform it, this way:

$('input[id=ans]').is(':checked');     

Solution 21 - Javascript

I found the following worked for me, with a mix of one of the answers, but with if statement.

$('#checkbox').on('change', function () {
 let checkbox_true;
  checkbox_true = $('#checkbox').prop("checked") ? 1 : 0 ;
   if (checkbox_true === 1) {
     alert('Yes');
   } else {
     alert('no');
   }
})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRajeevView Question on Stackoverflow
Solution 1 - JavascriptAndy MikulaView Answer on Stackoverflow
Solution 2 - JavascriptxaxxonView Answer on Stackoverflow
Solution 3 - JavascriptSimionBawsView Answer on Stackoverflow
Solution 4 - JavascriptStefan BrinkmannView Answer on Stackoverflow
Solution 5 - JavascriptcrashwapView Answer on Stackoverflow
Solution 6 - JavascriptMAnoj SarnaikView Answer on Stackoverflow
Solution 7 - JavascriptalphakevinView Answer on Stackoverflow
Solution 8 - JavascriptnobodyView Answer on Stackoverflow
Solution 9 - JavascriptRajesh RKView Answer on Stackoverflow
Solution 10 - JavascriptFareed AlnamroutiView Answer on Stackoverflow
Solution 11 - JavascriptSenthil Kumar BhaskaranView Answer on Stackoverflow
Solution 12 - Javascriptkmoney12View Answer on Stackoverflow
Solution 13 - JavascriptRobertView Answer on Stackoverflow
Solution 14 - JavascriptAmit MaruView Answer on Stackoverflow
Solution 15 - Javascriptsharif2008View Answer on Stackoverflow
Solution 16 - Javascriptuser8563382View Answer on Stackoverflow
Solution 17 - JavascriptPPBView Answer on Stackoverflow
Solution 18 - JavascriptNasarallaView Answer on Stackoverflow
Solution 19 - JavascriptAshok CharuView Answer on Stackoverflow
Solution 20 - JavascriptswathiView Answer on Stackoverflow
Solution 21 - JavascriptSpinstazView Answer on Stackoverflow