somesh somesh - 11 months ago 67
Javascript Question

How to pass all checkbox id's to jQuery change function

Here I am passing one checkbox ID like

filter_AFFILIATION_1
then O am getting this checkbox ID result.

How can I pass all check box ID's (
filter_AFFILIATION_1
,
filter_AFFILIATION_2
,
filter_AFFILIATION_3
,
filter_AFFILIATION_4
) at a time above javascript.



$(document).ready(function() {
$("#filter_AFFILIATION_1").change(function() {
var data = {
request: $('#filter_AFFILIATION_1').val()
};
$.ajax({
type: 'POST',
url: Routing.generate('listingpage'),
contentType: 'application/x-www-form-urlencoded',
data: data,
success: function(result, status, xhr) {
var div_options = document.getElementById("filter_AFFILIATION_1");
var inst_arr = JSON.parse(result);
var inst_val_arr = [];
for (var i = 0; i < inst_arr.length; i++) {
inst_val_arr[i] = inst_arr[i];
var affiliation = inst_val_arr[i].instaffiliation;

document.getElementById('aff').innerHTML = affiliation;

if (document.getElementById('filter_AFFILIATION_1').checked) {
$('#schooldata').html('');
} else if (!(document.getElementById('filter_AFFILIATION_1').checked)) {
alert('nothing');
}
}
},
error: function(xhr, status, error) {
console.log(status);
}
});
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="filter_AFFILIATION_1">
<input type="checkbox" id="filter_AFFILIATION_1" name="filter_AFFILIATION_1[]" value="1">ICSE
</label>
<label for="filter_AFFILIATION_2">
<input type="checkbox" id="filter_AFFILIATION_2" name="filter_AFFILIATION_2[]" value="2">CBSE
</label>
<label for="filter_AFFILIATION_3">
<input type="checkbox" id="filter_AFFILIATION_3" name="filter_AFFILIATION_3[]" value="3">State Board
</label>
<label for="filter_AFFILIATION_4">
<input type="checkbox" id="filter_AFFILIATION_4" name="filter_AFFILIATION_4[]" value="4">ISC
</label>




Answer Source

Make data as array data=[] and then push your values in it. Then in ajax data: data will send whole array. On the server you will get ids as data[0], data[1], data[2] and so on.

ok. i suggest making class for all check boxes and then get all id from that class like

var data= [];
    $('.filter_AFFILIATIONS').each(function () {
      data.push(this.id)
    }); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download