How to get multiple checkbox value in javascript and JQuery

On many websites, you find multiple check box forms to select your interest, or to select multiple options.

That can be created by using the HTML check box.

Here to make an input as a text box we need type="checkbox".

name is a name given to a text box

value property that specifies what value a text box holds.

id is used to uniquely identify this text box in HTML Document.

To get this text box value we can use JavaScript.

To get an element by Id we can use document.getElementById().

Inside getElementById() we pass the id of the element.

in the above code, we can see that check box id is checkbox1

and to get the value we can use the value property of getElementById()

how to get checkbox value in javascript with getelementbyid()

var bike = document.getElementById("checkbox1");         
     if(bike.checked){
         ele.push(bike.value);
   }

How to get multiple checkbox value in javascript with getElementById()

Here to select multiple checkboxes we selected each check box one by one.

Select Element by id using document.getElementById().

then check checkbox is checked or not.

If checked then push in the array.

How to get checkbox value in JQuery

JQuery makes code simple and easy, to select checkboxes we can use either name, id, or input type checkbox.

We can use any of the above ways as per need.

Here we will use checkbox id to select the checkbox.

You can download JQuery or include it from CDN

We checked check box is selected or not using ($('#checkbox1').is(":checked"))

Here checkbox1 is the id of the checkbox.

To get the value of the check box $('#checkbox1').val();

How to get multiple checkbox value in JQuery

$("input:checkbox[name=cb1]:checked").each(function(){                  
ele.push($(this).val());
});

Here Input check box with name cb1 is checked then for each element get checkbox value and push to array.

get multiple checkbox value using JQuery
Fig: get multiple checkbox value using JQuery

Example: how to get multiple checkbox” value in jquery using array

It is very easy to access multiple check box value using JavaScript and JQuery.

You can use these in your web page like in form and to capture user information.

How to store selected checkbox value in array in javascript

This SO answer may be helpful for this.

Check multiple checkboxes using jquery

This SO answer will be more helpful

Read More

  1. Add active class onclick
  2. JavaScript Multiplication Table
  3. isPrime() Javascript : Prime number program
  4. Form Handling and Validation
  5. Try Catch and Finally in JavaScript
  6. Math Object in JavaScript