Skip to content Skip to sidebar Skip to footer

How Can I Check A Checkbox When Another Checkbox Is Checked? [javascript]

So I have a little form which you can see at the bottom. And I want to know how I can make a JavaScript function where if I, for example, check checkbox 3 it will also check checkb

Solution 1:

<label><input onclick="onToggle()" type="checkbox" name="html3" value="html3" id="html3"/><span class="label-text"></span></label>

I think that way you can do it with plain Javascript

function onToggle(){
       if (document.querySelector('#html3').checked) {
          // if checked
          console.log('checked');
          document.getElementById("html2").checked = true
          document.getElementById("html1").checked = true
        } else {
          // if unchecked
          console.log('unchecked');
        }
}

Post a Comment for "How Can I Check A Checkbox When Another Checkbox Is Checked? [javascript]"