Nathan Kamenar
Nathan Kamenar

Reputation: 884

Toggling multiple checkboxes

I can't seem to get jquery to toggle multiple checkboxes. I want to grab all the checkboxes on the page with a certain name and toggle them. Here is what I am currently trying but it just generates the error below and I am not sure why. What am I missing here?

$("input[name=recurringGridCheckbox]").prop("checked", !(this.prop("checked")));

Object doesn't support property or method 'prop'

Upvotes: 2

Views: 115

Answers (2)

yezzz
yezzz

Reputation: 3020

.prop is a jquery method, so you must use it with a jquery object:

$("input[name='recurringGridCheckbox']").prop("checked", !$(this).prop("checked") );

Also, in specific cases the attribute value needs to be enclosed in quotes, so it may be good practice to always do that.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

There's a syntax error in rhe variable you use in the setter; this refers to the DOMElement, not the jQuery object and so does not have the prop() method available.

To fix this you can provide a function to the prop() method which will update the property based on its current state. Try this:

$("input[name=recurringGridCheckbox]").prop("checked", function(i, checked) {
    return !checked;
});

Upvotes: 6

Related Questions