how to get multiple checkbox value using jquery
Try this
<input name="selector[]" id="ad_Checkbox1" class="ads_Checkbox" type="checkbox" value="1" />
<input name="selector[]" id="ad_Checkbox2" class="ads_Checkbox" type="checkbox" value="2" />
<input name="selector[]" id="ad_Checkbox3" class="ads_Checkbox" type="checkbox" value="3" />
<input name="selector[]" id="ad_Checkbox4" class="ads_Checkbox" type="checkbox" value="4" />
<input type="button" id="save_value" name="save_value" value="Save" />
function
$(function(){
$('#save_value').click(function(){
var val = [];
$(':checkbox:checked').each(function(i){
val[i] = $(this).val();
});
});
});
To get an array of values from multiple checked checkboxes, use jQuery map/get functions:
$('input[type=checkbox]:checked').map(function(_, el) {
return $(el).val();
}).get();
This will return array with checked values, like this one: ['1', '2']
Here is working example on jsfiddle: http://jsfiddle.net/7PV2e/
You can do it like this,
$('.ads_Checkbox:checked')
You can iterate through them with each()
and fill the array with checkedbox values.
Live Demo
To get the values of selected checkboxes in array
var i = 0;
$('#save_value').click(function () {
var arr = [];
$('.ads_Checkbox:checked').each(function () {
arr[i++] = $(this).val();
});
});
Edit, using .map()
You can also use jQuery.map with get()
to get the array of selected checkboxe values.
As a side note using this.value
instead of $(this).val()
would give better performance.
Live Demo
$('#save_value').click(function(){
var arr = $('.ads_Checkbox:checked').map(function(){
return this.value;
}).get();
});