Categories
JavaScript Answers

How to get value of HTML Checkbox from onclick/onchange events with JavaScript?

Spread the love

To get value of HTML checkbox from onclick/onchange events with JavaScript, we use the value property.

For instance, we write

<input type="checkbox" onclick="onClickHandler()" id="box" />

to add a checkbox.

We set the onclick attribute to call the onClickHandler function when we click on the checkbox.

Then we write

function onClickHandler() {
  const chk = document.getElementById("box").value;
}

to define the onClickHandler function.

In it, we get the checkbox with getElementById.

Then we get the value of the checkbox from the value property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *