⊗jsPmFmChb 398 of 502 menu

Working with checkboxes in JavaScript

Now we will learn how to work with checkboxes. The checkbox can be in two states: checked and unchecked.

The checkbox is created like this:

<input type="checkbox" id="elem">

To make a checkbox checked, you need to write the attribute checked:

<input type="checkbox" checked id="elem">

For example, let's find out the state of our checkbox:

let elem = document.querySelector('#elem'); console.log(elem.checked);

Given a checkbox and two buttons. By clicking on the first button, set the checkbox to the checked state, and by clicking on the second - to the unchecked state.

Given a checkbox, a button and a paragraph. On clicking the button, if the checkbox is checked, output the word 'hello' into the paragraph, and if the checkbox is not checked, then the word 'bye'.

enru