I am having a bit of trouble trying to figure out how to get a certain part of my code to work.
<input type="checkbox" id="check_all_1" name="check_all_1" title="Select All" onclick="selectAll(document.wizard_form, this);"> <label for="check_all_1" onclick="toggleCheckbox('check_all_1'); return false;">Select All</label>
This is my HTML which works as it should (clicking the text will click the box). The javascript for it is pretty simple:
function toggleCheckbox(id) { document.getElementById(id).checked = !document.getElementById(id).checked; }
However I want the onclick to happen for the input when the label is what makes the checkbox to be clicked. At this current time the onClick js does not go. What is one suggestion on how to do this? I tried to add the onclick of the input to the onclick of the label but that doesn't work.
Any suggestions/solutions would be wonderful.
Create HTML Form Here you have two checkboxes as Present Address and Permanent Address with text boxes to display the address when clicked. Onclick function is used to call a JavaScript function to display the Address text when checked. With text box style as style=”display:none” display visibility property as hidden.
$(e. target) should get you the checkbox that has changed and fired the event. This code puts the listener on the document, so any checkboxes added to the document after doc ready will still have the event bubble up and fire your code.
Checkbox event handling using pure Javascript There are two events that you can attach to the checkbox to be fired once the checkbox value has been changed they are the onclick and the onchange events.
Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.
How about putting the checkbox
into the label
, making the label automatically "click sensitive" for the check box, and giving the checkbox a onchange
event?
<label ..... ><input type="checkbox" onchange="toggleCheckbox(this)" .....> function toggleCheckbox(element) { element.checked = !element.checked; }
This will additionally catch users using a keyboard to toggle the check box, something onclick
would not.
Label without an onclick will behave as you would expect. It changes the input. What you relly want is to execute selectAll()
when you click on a label, right? Then only add select all to the label onclick. Or wrap the input into the the label and assign onclick only for the label
<label for="check_all_1" onclick="selectAll(document.wizard_form, this);"> <input type="checkbox" id="check_all_1" name="check_all_1" title="Select All"> Select All </label>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With