0

Ok, So I have a list of checkboxes in a pop up. I grab two lists of jquery dom elements, a list of the checkboxes and a list of the checkboxes that are checked. The idea is that the user can change the checkboxes then if they want to, press a cancel button and it will revert to the original.

I get the two lists...

var allDropDownCheckboxes = $(this).next().find(".dropDownCheckbox");
var selectedCheckboxes = $(this).next().find(".dropDownCheckbox:checked");

On the cancel button being pressed I revert the checkboxes to their original state...

allDropDownCheckboxes.attr('checked', false);
selectedCheckboxes.attr('checked', true);

It unchecks them all but doesn't check the ones that were originally checked again...

Why?!

Thanks

2 Answers 2

1

try to use .prop() instead of .attr()

allDropDownCheckboxes.prop('checked', false);
selectedCheckboxes.prop('checked', true);
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use .prop() instead of .attr() to set the checked state of form elements, from the docs:

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method.

allDropDownCheckboxes.prop('checked', false);
selectedCheckboxes.prop('checked', true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.