Search code examples
javascriptjqueryhtmlw3c

Change the value of a checkbox inline using the onChange attribute


Am I doing this wrong or is this impossible? I want to set the value of the checkbox to "0" if unchecked, and "1" if checked

<input type="checkbox" onChange="function() {$(this).val(this.checked? '1': '0')};" />

NOTE: I know how to do it outside of the onChange event or by doing onChange="takeCareOfThis()", I'm looking to do it all inline


Solution

  • You just need to set the statement to be executed.

    <input type="checkbox" onChange="$(this).val(this.checked? '1': '0');" />