Ras4U Ras4U - 5 months ago 92
jQuery Question

this.elem.prop is not a function

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


my code is given below :

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);
});
});

Answer

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