Home > Web Front-end > JS Tutorial > body text

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

Linda Hamilton
Release: 2024-10-21 17:03:02
Original
976 people have browsed it

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>
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!