somesh somesh - 5 months ago 27
Javascript Question

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

Here I am passing one checkbox ID like

then O am getting this checkbox ID result.

How can I pass all check box ID's (
) at a time above javascript.

$(document).ready(function() {
$("#filter_AFFILIATION_1").change(function() {
var data = {
request: $('#filter_AFFILIATION_1').val()
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) {
} else if (!(document.getElementById('filter_AFFILIATION_1').checked)) {
error: function(xhr, status, error) {

<script src=""></script>
<label for="filter_AFFILIATION_1">
<input type="checkbox" id="filter_AFFILIATION_1" name="filter_AFFILIATION_1[]" value="1">ICSE
<label for="filter_AFFILIATION_2">
<input type="checkbox" id="filter_AFFILIATION_2" name="filter_AFFILIATION_2[]" value="2">CBSE
<label for="filter_AFFILIATION_3">
<input type="checkbox" id="filter_AFFILIATION_3" name="filter_AFFILIATION_3[]" value="3">State Board
<label for="filter_AFFILIATION_4">
<input type="checkbox" id="filter_AFFILIATION_4" name="filter_AFFILIATION_4[]" value="4">ISC

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 () {