Check if checkbox is checked with javascript

How to check if checkbox is checked in JavaScript

To check if a checkbox is checked in JavaScript, you can use the checked property of the HTML element. This property sets or returns the checked state of a checkbox.

Let us say that you have the following checkbox input field:

input type="checkbox" id="checkbox"> 

You can use the following code to check if the checkbox is checked or not:

const elem = document.querySelector('#checkbox') if (elem.checked)  console.log(`Checkbox is checked!`) > else  console.log(`Checkbox is not checked.`) > 

We used the querySelector() method to retrieve the checkbox element from DOM using its ID attribute value. Next, we inspected the value of the checked property to decide whether the checkbox was checked or not.

The checked property can also be used to change the checked status of a checkbox programmatically using JavaScript, as shown below:

// Mark checkbox as checked document.querySelector('#checkbox').checked = true // Uncheck checkbox document.querySelector('#checkbox').checked = false 

If you are using jQuery, the is() function can also be used to check if a checkbox is checked or not:

if ($('#checkbox').is(':checked'))  console.log(`Checkbox is checked!`) > else  console.log(`Checkbox is not checked.`) > 

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

How to check whether a checkbox is checked in JavaScript?

In this tutorial, we will learn to check whether a checkbox is checked in JavaScript. The checkbox is the input type in the HTML, which works as the selection box. The radio buttons which belong to the same group allow users to select only one value. Still, the checkbox which belongs to the same group allows users to select multiple values.

Also, you have many uses of checkboxes in your mind yourself. The HTML can add a checkbox to the webpage, but to add the behaviour to the checkbox, we must use JavaScript. Programmers can add different behaviours to the checkbox based on whether the checkbox is checked or not.

Here, we will learn to check for the single and multiple checkboxes is selected or not.

Check if a Single Check Box is Selected or not

In this section, we will learn to check whether the checkbox is checked or not. In JavaScript, we can access the checkbox element using id, class, or tag name and apply ‘.checked’ to the element, which returns either true or false based on the checkbox is checked.

Syntax

Users can follow the below syntax to check single checkbox is selected or not.

let checkbox = document.getElementById("checkbox_id"); let checkbox.checked; // it returns Boolean value

Example

In the below example, we have created the checkbox. Also, we have added the event listener to the checkbox. When the user changes the checkbox’s value, the event listener will be invoked. In the event listener, we will check for the checkbox is checked or not. If checkbox is checked, we will show some text to the div otherwise we will make the div empty.

html> head> title>Check whether the Checkbox is checked or not/title> /head> body> h2>Check whether the Checkbox is checked or not using i> .checked attribute /i>/h2> h4>Check the below checkbox to see the text div/h4> input type = "checkbox" id = "checkbox"> div id = "text"> /div> script> let checkbox = document.getElementById("checkbox"); checkbox.addEventListener( "change", () => if ( checkbox.checked ) text.innerHTML = " Check box is checked. "; > else text.innerHTML = ""; > >); /script> /body> /html>

In the above output, users can see that when they check the checkbox, it shows a message “checkbox is checked.” When they uncheck the checkbox, it shows nothing.

Check if Multiple Checkboxes Are Selected or not

It is simple to add the behaviour to a single checkbox. On many websites, you have seen that when you see the popup to accept the terms & conditions, it has multiple checkboxes and when you select all checkboxes, only it enables the accept button.

Here, we will do the same thing. We will create the multiple checkbox and check for all checkbox whether it is checked or not and on the basis of that, we will enable the button.

Syntax

let checkbox = document.getElementsByName( "checkbox" ); let button = document.getElementById( "btn" );
for ( let i = 0; i  checkbox.length; i++ )  checkbox[i].addEventListener( "change", () =>  >); >
button.disabled = false; for ( let i = 0; i  checkbox.length; i++ )  if ( checkbox[i].checked == false ) // if any single checkbox is unchecked, disable the button. button.disabled = true; >

Example

In the example below, we have created the three checkboxes with the same name, which means all belong to the same group. Also, we have created the button in HTML and the accessing button and checkbox using the id and name in JavaScript.

We have added an event listener in all checkboxes. When any checkbox value changes, it will check whether all checkbox is checked or not. If all checkbox is checked, the event listener enables the button. Otherwise, the button remains disabled.

html> head> /head> body> h2>Check whether the Checkbox is checked or not/h2> h4>Check the below all checkboxes to enable the submit button./h4> input type = "checkbox" name = "checkbox"> input type = "checkbox" name = "checkbox"> input type = "checkbox" name = "checkbox"> button id = "btn" disabled> enable button/button> script> // access elements by id and name let checkbox = document.getElementsByName("checkbox"); let button = document.getElementById("btn"); // Initialilly checkbox is disabled for (let i = 0; i checkbox.length; i++) // iterate through every checkbox and add event listner to every checkbox. checkbox[i].addEventListener( "change", () => button.disabled = false; // if all checkbox values is checked then button remains enable, otherwise control goes to the if condition and disables button. for (let i = 0; i checkbox.length; i++) if ( checkbox[i].checked == false ) button.disabled = true; > >); > /script> /body> /html>

In the above output, users can see that when they check all the checkboxes, button will be enable, otherwise button remains disabled.

In this tutorial, we have learned how we can check whether single or multiple checkboxes are selected or not. We have added the different behaviour to the web page according to the checkbox selection value.

Also, users can use the JavaScript libraries such as jQuery, so users need to make less effort to check for multiple checkboxes.

Источник

How to Check if Checkbox is Checked Using JavaScript

The checkbox is an HTML input type that acts as a selection box. It is utilized for selecting or deselecting a certain option. Checkboxes are frequently utilized to let visitors engage with web pages and often POST data to a backend by checking the box that applies to a certain condition.

This manual will demonstrate the technique to verify if the checkbox is checked or not.

How to Check if Checkbox is Checked Using JavaScript?

To determine if the checkbox is checked, use the “checked” property. As we know, each element contains a few characteristics that allow Javascript to control it in certain ways and the checkbox’s checked property is one of these. The checked property gives the boolean value true or false when a checkbox is selected.

Example 1: Using onclick() Event For Checking if Checkbox is Checked
We will first create a checkbox attaching the “onclick()” event that will invoke the user-defined “boxChecked()” function to check whether the checkbox is checked or not:

In the JavaScript file, we will define a function named “boxChecked()”, which will first fetch the checkbox’s id using the “getElementById()” method and then apply the checked property in such a way that the checked returned value is set to “true” if it is marked as checked. Then, an alert will be shown with the message “The checkbox is CHECKED! Thanks for accepting:”:

function boxChecked ( ) {
var checkBox = document. getElementById ( «Checkbox» ) ;
if ( checkBox. checked == true ) {
alert ( «The checkbox is CHECKED! Thanks for accepting:» ) ;
}
}

The corresponding output is shown below:

Example 2: Using addEventListener() Method For Checking if Checkbox is Checked
Here, we will create a checkbox without an onclick() event. Then, we will add a paragraph, and its display is set as “none” until the checkbox is unchecked. In the other case, when the checkbox is marked as checked, its added text will be displayed in green color:

You can also verify whether the checkbox is checked or not without using the onclick() event. Here, we will use another method called “addEventListener()” with the “querySelector()” method using the checkbox’s checked property. This will work in such a way that the querySelector() method will first select the first element matched with the added id, and then the addEventListener() method will associate the “click” event with it:

var text = document. getElementById ( «text» ) ;
document. querySelector ( ‘#Checkbox’ ) . addEventListener ( ‘click’ , ( event ) => {
if ( event. target . checked ) {
text. style . display = «block» ;
}
} )

It can be seen that, when the checkbox is marked, the added content is displayed in green color:

Everything you need to know about how to determine if a checkbox is marked or not has been provided to you.

Conclusion

To determine if the checkbox is checked, utilize the checkbox’s “checked” property. The checked property outputs a boolean value true if it is checked; else, it gives false. For the verification, you can use two different procedures; one is the onclick() event, and the other is addEventListener() method. In this manual, we have described the procedure to verify whether the checkbox is checked or not using JavaScript.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Input Checkbox checked Property

The checked property sets or returns the checked state of a checkbox. This property reflects the HTML checked attribute.

Browser Support

Syntax

Property Values

Technical Details

Return Value: A Boolean, returns true if the checkbox is checked, and false if the checkbox is not checked

More Examples

Example

Find out if a checkbox is checked or not:

Example

Use a checkbox to convert text in an input field to uppercase:

Example

Several checkboxes in a form:

var coffee = document.forms[0];
var txt = «»;
var i;
for (i = 0; i < coffee.length; i++) if (coffee[i].checked) txt = txt + coffee[i].value + " ";
>
>
document.getElementById(«order»).value = «You ordered a coffee with: » + txt;

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  Static method and class in php
Оцените статью