I need to select all cities under states (eg. India) listed. I put a select all:
<input type="checkbox" class="selectall" /> Select All
Then the below error is appearing:
this.elem.prop is not a function
This is my code:
jQuery('.selectall').click(function() {
stop = false;
jQuery(this).closest('div').nextAll().each( function() {
elem = jQuery(this).find('input');
console.log(elem);
if (stop || elem.hasClass('selectall')) {
stop = true;
return;
}
else
elem.prop("checked", true);
});
});
The prop()
method was only added in jQuery 1.6. In previous versions you should use attr()
.
elem.attr("checked", true);
Note about other answers, elem
is already a jQuery object as it was defined on this line: elem = jQuery(this).find('input');
, therefore the prop()
method would be available if the jQuery version supports it.
UPDATE
To toggle the checkbox, use this:
if (stop || elem.hasClass('selectall')) {
stop = true;
return;
}
else
elem.prop("checked", !elem.is(':checked')); // toggle the checkbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With