-1

How can I get a true or false from a checkbox in a form?

I want to do this without jQuery

I tried to get it with getElementById but it does not give anything.

<p class="yellow"><input type="checkbox" id="accept">Ik accepteer</p>

function required() {
    var checkPrivacy = document.getElementById("accept").innerHTML
    alert(checkPrivacy)
    if (checkPrivacy == false)
    {
        alert("Option 1");
    } else {
        alert("Option 2")
    }
}
Robin
  • 11
  • 4

2 Answers2

1

You can use the "checked" property

function isChecked(checkboxId) {
    document.getElementById(checkboxId).checked;
}

You can also set it

function check() {
    document.getElementById("myCheck").checked = true;
}
dasfacc
  • 148
  • 2
  • 8
0

Try this:

<p class="yellow"><input onClick="handleSelectedItems(e)" type="checkbox" id="accept">Ik accepteer</p>

const handleSelectedItems = (e) =>{
const selected = e.target.checked; //true or false

if(selected){
   // do something
} else{
   // do something else
}
}
nurmdrafi
  • 419
  • 4
  • 11