14

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);
    });
});
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
Ras4U
  • 440
  • 2
  • 10
  • 30

2 Answers2

29

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
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
0

Your JQuery version must be older than v1.6. See the following JQuery documentation link that states that the "prop" function was only added from v1.6 onwards.

http://api.jquery.com/prop/

So the solution to your problem is to use "attr" function instead of "prop".

Apparently, attr retrieves attributes (which are the initial String values of the HTML elements) and prop retrieves properties (which could be the DOM manipulated values and could be of any type such as string, boolean, etc). Read the following Stackoverflow link for more detailed explanation on the difference between the two.

.prop() vs .attr()

Community
  • 1
  • 1
VHS
  • 8,698
  • 3
  • 14
  • 38