Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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" />
share|improve this question
1  
.val() is to get the value attribute, not the check status. The value attribute can be anything, it doesn't need to be boolean. – Pablo Pazos Nov 16 at 4:07

17 Answers 17

up vote 840 down vote accepted

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

More information here.

share|improve this answer
$("#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')
share|improve this answer
8  
.prop() for jQuery 1.6+ library; works perfect! Thank you – peevesy Jul 21 '14 at 12:22
    
Yeah, .prop() is the best option. I'm using a lib that styles checkboxes and .attr() didn't work. Cheers – kabadisha Jun 5 at 16:25

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

It returns a boolean value.

share|improve this answer
// use ternary operators
$("#ans").is(':checked') ? 1 : 0;
share|improve this answer
5  
What for? is(':checked') already return a boolean value – Loic Coenen Dec 14 '15 at 12:32
    
@Loic to set his value using the terse ternary syntax, eg: : a = ischecked ? 1 : 0 – user1135300 Jul 29 at 12:32

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

share|improve this answer
9  
...because jQuery is known as "the write more do less" library – myshadowself Nov 13 '13 at 11:24
    
long story you have gone to far JQUERY want developer to write less and do extra more. simple problem deserve simple solution – ShapCyber Jul 1 '15 at 21:07
    
yes, agreed, write less, do more. you write the extension once, and for the rest of time, you only call the API $(someradio).realVal(), you don't need write a extension many times. – antmary Jun 12 at 9:08

Stefan Brinkmann'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'

If this is helpful, please upvote Stefan Brinkmann's answer.

share|improve this answer

Try this :)

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

share|improve this answer

You can also use:

$("#ans:checked").length == 1;
share|improve this answer
$('input:checkbox:checked').val();        // get the value from a checked checkbox
share|improve this answer
<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';
share|improve this answer

Use:

$("#ans option:selected").val()
share|improve this answer
    
if it's not selected, then the first part wouldn't return anything, right? – xaxxon Jan 27 '11 at 6:06
    
@Senthil, OP is not asking to get the value of a selected checkbox, rather return 1 if it is selected – Om Shankar Jul 29 '12 at 15:51
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"));
share|improve this answer

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.

share|improve this answer
    
The first one should be (':checked'), doesn't work otherwise. – Arie Livshin Oct 5 at 11:18

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.

share|improve this answer

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);
share|improve this answer

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

$("input[type='checkbox']").prop("checked");
$("input[type='checkbox']").is(":checked");
share|improve this answer

If you want integer value of checked or not try:

$("#ans:checked").length

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.