Get Checked Checkboxes Values jQuery

So you need to know which checkboxes are checked?

  • This tutorial assumes you already know the .click trigger of jQuery

jQuery

Do not forget to include the jQuery

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

The Selector

Using the :checked selector we can filter checkboxes that are checked.

$(':checked')

This selects all checked input elements

Gathering the values

Iterating throught the checkboxes

$.each(fn(index,object))

The $.each is a generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

Final Code

<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<input type="checkbox" value="check1" /> Check 1<br /> <input type="checkbox" value="check2" /> Check 2<br /> <input type="checkbox" value="check3" /> Check 3<br /> <input type="checkbox" value="check4" /> Check 4<br /> <input type="checkbox" value="check5" /> Check 5<br /> <input type="checkbox" value="check6" /> Check 6<br /> <a id="gatherValues"></a>Gather
<script type="text/javascript">
	$('a#gatherValues').click(function(){
	  var checked_values = [];
	  $(':checked').each(function(index,object){
		checked_values.push($(object).val());
	  });
	  alert(checked_values.join("\n"));
	});
  </script>

The code will gather all checked values from the checkboxes

Try US For Free:

Start Your Free 14 Day Trial Build Your First For FREE Right Now!
No obligations, no contracts, cancel at any time, 100% risk free.
  • Simple drag and drop webpage editor!
  • Quickly build sales funnels that convert!
  • Smart shopping cart with 1 click upsells!
  • Email and Facebook Marketing Automation!
  • Everything organized in one simple dashboard!
 
WE Give You EVERYTHING You Need To Market, Sell and Deliver Your Products Online!