0

I want to fetch multiple checkboxes values from one div. My code executes successfully on firefox but in other browsers it doesn't work. My code looks like

var amenity_array = [];
var listofParameters = $("#room-amenity input:checkbox");
for (var index in listofParameters) {
    if ($(listofParameters[index]).attr('checked')) {
        var ste = $(listofParameters[index]).attr('value');
        amenity_array.push(ste);
    }
}
alert(amenity_array);

in the above code amenity_array alerts within the braces but out of this it doesn't work on chrome.

Tushar
  • 85,780
  • 21
  • 159
  • 179

1 Answers1

2

Couple of suggestions/bugs:

  1. Make sure your selector is correct to select checkboxes
  2. Use :checked to select only the checkboxes that are checked
  3. Don't use for...in for looping over array
  4. You can use each() to get the checked checkboxes and add them in your array
  5. Make sure that at-least one checkbox is selected, otherwise the array will have no elements in it

Code:

var amenity_array = [];

$('#room-amenity input:checkbox:checked').each(function() {
    amenity_array.push($(this).val());
});

console.log(amenity_array);
Community
  • 1
  • 1
Tushar
  • 85,780
  • 21
  • 159
  • 179