Home  >  Article  >  Web Front-end  >  How to Get the Value of a Selected Checkbox in JavaScript?

How to Get the Value of a Selected Checkbox in JavaScript?

Linda Hamilton
Linda HamiltonOriginal
2024-10-21 17:03:02980browse

How to Get the Value of a Selected Checkbox in JavaScript?

How to Retrieve the Value of a Selected Checkbox in JavaScript

Checkboxes are used to select one or more options from a list. In the provided code snippet, there are two checkbox inputs with values "3" and "1." To retrieve the value of the selected checkbox, we can use JavaScript.

Solution:

To obtain the value of the sole checked checkbox, use the following code:

<code class="javascript">document.querySelector('.messageCheckbox').checked;</code>

In this code, .messageCheckbox selects the checkbox with the specified class name. The .checked property returns a Boolean value indicating whether the checkbox is checked (true) or not (false).

The above is the detailed content of How to Get the Value of a Selected Checkbox in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn