Its very easy to add a checkbox in HTML but once it has a tick in the box how can we trigger an action to happen.
This would usually be on a click event of a button, not when the button is ticked, probably use on focus of that box or click event of that box, but not tested.
This will test using jQuery if the checkbox is checked or not.
If I have a checkbox on a form and want to check if it was enabled or checked/ticked I can do so by the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<input type="checkbox" id="agree" name="accept" /> <script> $(document).ready(function() { $("#agree").click(function() { if ($("input[type=checkbox]").prop( ":checked")) { alert("Check box in Checked"); } else { alert("Check box is Unchecked"); } }); }); </script> |
You could also try this, but after revisiting the code above only worked – but it did not turn it off? – Use the code below…
1 2 3 4 5 6 7 |
if(document.getElementById('agree').checked) { $("#mybanner").show(); } else { $("#mybanner").hide(); } |
Best way – it’s Javascript!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<input type="checkbox" id="agree" name="accept" onclick="myFunction()"/> <script> function myFunction() { var checkBox = document.getElementById("agree"); if (checkBox.checked == true){ nextstep.style.display = "block"; } else { nextstep.style.display = "none"; } } </script> |