I have a small html form with three checkboxes, one in each row. I want to have a javascript confirm box show when the user unchecks the box, and then if the user selects cancel, the checkbox remains checked. I’ve searched this site and a few others but almost everything was to do with the checkbox being checked, like if they had to agree to some terms or something. I am very new to Javascript however I tried to make a function based on what I though it should look like, but it doesn’t work.
Here is my form:
<form action="" method="post">
<table id="table">
<tr>
<td>Checkbox One</td>
<td align="center">
<input type="checkbox" name="check1" checked="checked" onchange="cTrig(this.name)"></input>
</td>
</tr>
<tr>
<td>Checkbox Two</td>
<td align="center">
<input type="checkbox" name="check2" checked="checked" onchange="cTrig(this.name)"></input>
</td>
</tr>
<tr>
<td>Checkbox Three</td>
<td align="center">
<input type="checkbox" name="check3" checked="checked" onchange="cTrig(this.name)"></input>
</td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="submit" value="submit"></input>
</td>
</tr>
</table>
</form>
And here is the function I tried, but doesn’t work:
function cTrig(name) {
if (name.checked == true) {
confirm("Are you sure you want to do this?");
return true;
} else {
return false;
}
}
Here is a jsfiddle
I would prefer something in Javascript as I want to become more familiar with that language before getting into jquery, but if it has to be done in jquery, then that is fine. Cheers.
Try This Way
By Using Element Id
Working Demo
By Using Name
Demo Using element name