Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkbox value 0 or 1

In many cases, I have checkboxes as below

 <input type="checkbox" name="custom7" value="1" id="custom7"
 checked="checked"> <label for="custom7">Email me more info?</label>

Now when the checkbox is checked the value should be 1 as in value=1 . If the checkbox is unchecked, I want the value to be zero as in value=0. How can I do this?

like image 323
Hello Universe Avatar asked Aug 18 '13 04:08

Hello Universe


1 Answers

Just to be a butt and offer a slightly shorter answer:

$('input[type="checkbox"]').change(function(){
    this.value = (Number(this.checked));
});
like image 74
phatskat Avatar answered Sep 25 '22 10:09

phatskat