Alex Rivera | Logout

Testing if a checkbox is checked with jQuery

Asked 2011-01-27T05:59:52.833
597

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

1 Answer

2

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);
answered 2013-02-22T06:58:49.120

Your Answer