Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get checked checkboxes from name[]

I have checkboxes like so:

<ul id="searchFilter">
    <li><input type="checkbox" name="price[]" class="cb_price" value="1"> $200,000 to $299,999</li>
    <li><input type="checkbox" name="price[]" class="cb_price" value="3"> $300,000 to $399,999</li>
    <li><input type="checkbox" name="price[]" class="cb_price" value="5"> $400,000 to $499,999</li>
    <li><input type="checkbox" name="price[]" class="cb_price" value="8"> $500,000+</li>
</ul>

How would I alert the price[] to see what is checked? I am very new at jquery :(

like image 334
user3723240 Avatar asked Jan 11 '23 05:01

user3723240


2 Answers

First, you can get the checkboxes by name:

var checkboxes = $('input[name="price[]"]');

Then, to get the values of the checked ones, you can filter by the pseudo selector :checked, and then collect their values:

checkboxes.filter(":checked").map(function () {
    return this.value;
}).get()

DEMO: http://jsfiddle.net/Fn9WV/


References:

  • jQuery().filter() - http://api.jquery.com/filter/
  • jQuery().map() - http://api.jquery.com/map/
like image 134
Ian Avatar answered Jan 25 '23 01:01

Ian


You can try this:-

var selected = [];
$('[name="price[]"]:checked').each(function(checkbox) {
selected.push(checkbox);
});
like image 45
Indranil Mondal Avatar answered Jan 24 '23 23:01

Indranil Mondal