Here I am trying to change the value of the following check box while clicking on it. In the code below I tried to change the value of the checkbox to 1 and change the value to 0 when unchecked. But it takes only the false condition, when the checkbox is unchecked the value changes to 0 but when checked its not changing to 1. Any suggestions how to fix this out ?
<input type="checkbox" id="read" name="permission" onClick="($(this).checked)?$(this).attr('value',1):$(this).attr('value',0)"/>
I don't really understand why you would want to do this (the checkbox's value won't be submitted anyways when it's unchecked).
If you really need to, you should do this:
onclick="$(this).attr('value', this.checked ? 1 : 0)"
onclick="$(this).val(this.checked ? 1 : 0)"
You are using
$(this).checked to get the state of your checkbox. The jQuery object (the one that's returned by the
$ function) does not have a
checked property, so it will be
undefined is a falsy value, that's why your checkbox's value is always