- How to find all Checked checkboxes in jQuery? Example Tutorial
- jQuery Example to get All checked checkboxes in HTML
- "
- Output
- How To Get All Checked Checkbox Value In Javascript
- Selecting All Checkboxes In A Group
- Selecting Checked Checkboxes Of A Group
- 1. Selecting using the checked property of the checkbox
- 2. Selecting using CSS selector :checked
- Select All Checkboxes Of Entire Document
- Conclusions
- Html checkbox get all checked
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
- How to get all checked checkbox values using JavaScript
How to find all Checked checkboxes in jQuery? Example Tutorial
Hello guys, suppose you have multiple checkboxes in your HTML page and you want to retrieve all checkboxes which are checked? How will you do that in jQuery? Well, you can use the pseudo selector like :checked to get all checked checkboxes. This selector checks for the checked property of the checkbox and returns only those checkboxes which have this property. For example, the following jQuery selector will return all the checkboxes which are checked:
$(‘input[type=checkbox]:checked’)
In this, we are first selecting all input elements where type is a checkbox and then adding: checked to filter only those which are checked.
On the other hand, If you just use:
All checkboxes will be selected, I mean, both checked and unchecked. Remember, this returns an array of elements, so you need to use each() jQuery function if you want to do something with them like printing their value.
Btw, if you are new to Web Development or maintaining a legacy project which is written using HTML and JavaScript, it pays a lot to learn new technologies and frameworks like jQuery, Angular, React, etc and that’s why I recommend all Web developers to join The Web Developer Bootcamp course by Colt Steele on Udemy.
I have personally learned a lot of stuff from this course and it’s awesome to get an overview of all important technologies in one place.
jQuery Example to get All checked checkboxes in HTML
In this example, I have an HTML page with three checkboxes read, write and speak. You might have seen them in the job portal where they ask about your language proficiency. Out of these three checkboxes, I have both read, write checked, and speak is unchecked.
To demonstrate how to use the jQuery selector to get all checked checkboxes, I have written some code on the $(document).ready() function. Btw, if you don’t know what is document ready function and how jQuery works, I suggest you first go through a jQuery fundamental course like The Complete jQuery Bootcamp on Udemy.
Anyway, that code selects all checked checkboxes after the page is loaded and print’s their id and status by using the attr() and val() function. I have used each() function to iterate over each of those checked boxes.
html> head> title>How to find all checked checkboxes in jQuerytitle> head> body> h2>Displaying all checked checkbox value in jQueryh2> Read: input type="checkbox" id="read" checked="checked" /> Write: input type="checkbox" id="write" checked="checked" /> Speak: input type="checkbox" id="speak" /> div id="output">div> script src="http://code.jquery.com/jquery-1.6.2.min.js">script> script> $(document).ready(function () $('input[type=checkbox]:checked').each(function () var status = (this.checked ? $(this).val() : ""); var id = $(this).attr("id"); $('#output').append(""
+ id + " : " + status + ""); >); >); script> body> html>
Output
When you open this HTML file in a browser or Visual Studio Code, you will see that it will display the Id of two checked checkboxes which are «read», and «write» in our case. Here is the screenshot from the Microsoft Edge browser:
That’s all about how to get all checked checkboxes of a page in jQuery. Once you are able to find them you can do whatever you want to do like printing their values or disabling them or clearing the selection. Just remember to use each() method, so that you can iterate over each checkbox and perform some action.
- The Web Developer Roadmap (roadmap)
- 5 Free jQuery Courses for Web Developers (join)
- Top 5 jQuery Books Every Web Developer Should Read? (see here)
- 20 jQuery Interview Questions for Programmers (questions)
- How to get the current URL Parameter using jQuery? (solution)
- How to use multiple jQuery Date picker elements in one HTML page? (answer)
- How to reload/refresh a page using jQuery? (answer)
- How to prevent double submission of POST data using JavaScript? (solution)
- How to check and uncheck checkboxes using jQuery? (tutorial)
- 10 Examples of jQuery selectors for Web Developers (tutorial)
- 10 Frameworks Every Web Developer Should Learn (frameworks)
- Top 5 Courses to Learn Web Development (courses)
Thanks for reading this article so far. If you like this jQuery tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.
P. S. — If you are self-learning jQuery online and looking for a comprehensive course to further boost your learning, I suggest you join The Complete jQuery Bootcamp, which will not only teaches jQuery but also provides valuable hands-on experience by building 20 real projects.
How To Get All Checked Checkbox Value In Javascript
In this article, you will learn how to get all checked checkbox value in Javascript. We will cover all use cases of checkboxes and get their data in Javascript.
Checkboxes are boxes that are used to select or deselect an option. They are generally used in forms, such as checkboxes in a form to select a single option or multiple options.
They are different from radio buttons in which only one option can be selected at a time. Checkboxes can be used to select multiple options.
Checkbox is an input element with type=»checkbox» . They have a name attribute that is used to group multiple checkboxes for one purpose and a value attribute that is used to store the value of the checkbox.
You can use the name property to select all checkboxes of the same group.
Selecting All Checkboxes In A Group
To select all checkboxes of a group you can use their name attribute.
To select an element by element’s name attribute you can use document.getElementsByName() method. It returns an array of elements with the specified name attribute.
In the example below, we are selecting all checkboxes of a group with a single click of a button and storing their values in an array.
Choose Language you can work with:
HTML
CSS
Milk (food)
JavaScript
PHP
Python
Vegetable (food)
Ruby
You can see in the output below only checkboxes that belong to the group language are selected.
Choose Language you can work with:
HTML
CSS
Milk (food)
JavaScript
PHP
Python
Vegetable (food)
Ruby
Select All
Another way to select checkboxes of a group is by using the querySelectorAll() method. It returns a NodeList of elements with the specified name attribute.
To select checkboxes of group language you can apply document.querySelectorAll(‘input[name=»language»]’) .
// selecting all checkboxes // of group language using querySelectorAll() var checkboxes = document.querySelectorAll('input[name="language"]'); var values = []; // looping through all checkboxes for (var i = 0; i < checkboxes.length; i++) < checkboxes[i].checked = true; values.push(checkboxes[i].value); >alert(values);
Selecting Checked Checkboxes Of A Group
When the user has given their choice in the checkboxes then you need to get those data and perform some action on it.
To get all checked checkboxes values in Javascript you can follow 2 ways:
- Selecting using the checked property of checkbox.
- Selecting using CSS selector :checked .
1. Selecting using the checked property of the checkbox
To select all checked checkboxes of a group first select all checkboxes of a group and then loop through all checkboxes and get their checked property value.
If the return value is true then the checkbox is checked. You can store it in an array for further use.
// selecting all checkboxes // of group language using querySelectorAll() var checkboxes = document.querySelectorAll('input[name="language"]'); var values = []; // looping through all checkboxes // if checked property is true then push for (var i = 0; i < checkboxes.length; i++) < if (checkboxes[i].checked == true) < values.push(checkboxes[i].value); >> alert(values);
Choose the language you can work with and click the button to get selected boxes.
HTML
CSS
JavaScript
PHP
Python
Ruby
Get Selected Options
2. Selecting using CSS selector :checked
There is another way to select all checked checkboxes of a group. It is by using a CSS selector.
:checked is a pseudo CSS selector which can select the checkbox which is checked.
Use this with the querySelectorAll() method to select all checkboxes of a group.
// selecting all checkboxes // of group language using :checked pseudo selector var checkboxes = document.querySelectorAll('input[name="language"]:checked'); // get values of all checked checkboxes var values = [. checkboxes].map(checkbox => checkbox.value); alert(values);
Choose Language you can work with then click button.
HTML
CSS
JavaScript
PHP
Python
Ruby
Get Selected Options
Select All Checkboxes Of Entire Document
There will be a similar approach to select all checkboxes of the entire document. Here instead of targeting a group of checkboxes, we will target all checkboxes of the entire document.
While using the querySelectorAll() method we can target all checkboxes of the entire document by using selector input:checked .
// selecting all checkboxes // of entire document using querySelectorAll() var checkboxes = document.querySelectorAll('input:checked'); // get values of all checked checkboxes var values = [. checkboxes].map(checkbox => checkbox.value); alert(values);
If there is any checkbox in this document checked then it will be shown.
HTML
Milk
Chips
Shampoo
CSS
Bread
Candy
Get Selected Options
Conclusions
In this short guide, we have seen how to select all checked checkboxes value in JavaScript with different case scenarios. Like selecting all checkboxes of a group, selecting all checked checkboxes of a group, selecting all checked checkboxes of the entire document, etc.
Html checkbox get all checked
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter
How to get all checked checkbox values using JavaScript
Retrieving the values from a collection of checkboxes is not as straightforward as you would expect. In this article, I will show you one way you can accomplish this.
Let’s say you have the HTML below and you want to retrieve the values of the checkboxes a user has selected.
div class="filter"> Filter by quarter: ul> li> input type="checkbox" value="1" name="q1" id="q1" /> label for="q1">Q1label> li> li> input type="checkbox" value="2" name="q2" id="q2" /> label for="q2">Q2label> li> li> input type="checkbox" value="3" name="q3" id="q3" /> label for="q3">Q3label> li> li> input type="checkbox" value="4" name="q4" id="q4" /> label for="q4">Q4label> li> ul> div>
We can use querySelectorAll and look for input elements with the type checkbox to get all checkboxes but we only want the checkboxes a user has selected. We can get the selected checkboxes by using the :checked pseudo class.
document.querySelectorAll('input[type="checkbox"]:checked')
Since querySelectorAll returns an HTMLCollection will need to convert it to an array and map over that array to get the value of the checked checkboxes.
We will use event delegation to listen for checkbox changes. Event delegation enables us to add the event listener to a parent element (the ul ) instead of each child element ( input[type=»checkbox»] ). For more information on event delegation, I highly recommend David Walsh’s blog post “How JavaScript Event Delegation Works”
const ul = document.querySelector('ul') let selected = []; ul.addEventListener('change', event => if (event.target.type === 'checkbox') const checked = document.querySelectorAll('input[type="checkbox"]:checked') selected = Array.from(checked).map(x => x.value) > >)
Any time a checkbox is clicked, the selected array will be updated. If we select the first and last checkbox selected will contain [«1», «4»] .
Joshua Colvin is a UI Software Engineer specializing in building component libraries. He lives with his wife and two kids in Michigan.