Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery checkbox val() always return on

Tags:

jquery

Hi after a lot of search I got a method to check my checkbox. But I dont know what is the problem. Its val() always return on. This is happening when I check it and also when I uncheck it. I suppose it should return null when unchecked. Help

$(document).ready(function(){
$('#ch').change(function(){
    alert($(this).val());
});
 });

and the html

<input type="checkbox"  id="ch"/> Check it out...
like image 959
Shameer Avatar asked Sep 15 '13 04:09

Shameer


People also ask

Does check always return false?

checked always returns false, the html is correct. The HTML may be correct but you need to add it to your question.

How to check checkbox checked value in jQuery?

To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);

How to check if 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.


1 Answers

You have set of option to check if your checkbox is checked or unchecked,

alert($(this).is(":checked")); ////return true if checked

Or

alert(this.checked); //return true if checked

Or

<input type="checkbox" id="ch" checked/>
alert($(#ch).attr( "checked")); //return checked if checked 

Or

alert($(this).prop( "checked")); //return true if checked
like image 187
Deepak Ingole Avatar answered Sep 22 '22 23:09

Deepak Ingole