JavaScript to update a text box when a checkbox is checked/unchecked


Here is a simple form with 2 fields. "box1" is a text box and there is a checkbox named "cboxToggle".

When the checkbox is clicked, JavaScript updates the value of the text box.

Toggle Box

Here is the code for the form and the JavaScript that reads the checkbox and updates the text box's value.

<form id="form1" name="form1" action="" method="post">

<p><input type="text" id="box1" value="Off"/></p>

<p>Toggle Box <input type="checkbox" id="cboxToggle" name="cboxToggle" onClick="Toggle();"></p>

<script>

Toggle();

function Toggle(){

if (document.form1.cboxToggle.checked){

document.form1.box1.value='On';

}

else{

document.form1.box1.value='Off';

}

}

</script>

</form>