There are multiple objects in a div. I want to get the id of all those elements by their class. the number of elements can vary. I am doing it as
arr= $(".listitem #checkBox").hasClass('checkedItem').attr('id');
but it return only the first item
and if I use it as
arr= $(".listitem #checkBox").hasClass('checkedItem').map(function() {
return this.id;
}).get();
The error on console is Object true has no method 'map'
How I can get the Ids of all CheckedItems
The
.hasClass()method returns a boolean indicating whether any elements in the jQuery object have that class, and a boolean doesn’t have a.map()method.Instead, make the class part of the selector, so that the jQuery object contains only elements with that class:
Note that your original selector
".listitem #checkBox"should only ever match one or zero elements, because id is supposed to be unique. For that reason I have assumed above that you are trying to check elements that are descendants of#checkBox. Let me know the structure of your html and I can tweak the selector to match…