Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML checkbox onclick called in Javascript

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.

like image 467
Craig Avatar asked Jul 07 '10 18:07

Craig


People also ask

How call JavaScript checkbox checked in HTML?

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.

How do you call a function on a checkbox click?

$(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.

What is the event of checkbox?

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.

How do I check if a checkbox is checked in JavaScript?

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.


2 Answers

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.

like image 129
Unicron Avatar answered Sep 23 '22 21:09

Unicron


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> 
like image 39
25 revs, 4 users 83% Avatar answered Sep 19 '22 21:09

25 revs, 4 users 83%