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.

2
  • Don't use alert. Use console.log() Commented Aug 27, 2015 at 6:31
  • It also doesn't work. I want to use these amenities in another place. Commented Aug 27, 2015 at 6:35

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@RAVIPRAKASHAWASTHI You need to open browser console to see result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.