Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting checkbox state from JavaScript #4164

Closed
MikeGWem opened this issue Feb 29, 2016 · 3 comments
Closed

Setting checkbox state from JavaScript #4164

MikeGWem opened this issue Feb 29, 2016 · 3 comments

Comments

@MikeGWem
Copy link

I have a number of checkboxes on a form and wish to set and unset some from JavaScript after the page has loaded.

setting the "checked" property of the Input element in the DOM has no effect.
calling componentHandler.upgradeElement() after setting the checked state has no effect.

I looked at all of the issues tagged for checkbox but did not see a solution. Feels a tad fundamental - I am assuming there are methods to interact with the MDL upgraded HTML objects.

@MikeGWem
Copy link
Author

Got there in the end by re-setting the is-checked class on the parent tag

var te = $('#some-checkbox').prop("checked", false).parent();
te.removeClass('is-checked');

is a jQuerified version

@sachin8080
Copy link

Thanks @MikeGWem for your solution. I was facing the same issue. I feel there should be a more generic way of doing this and componentHandler.upgradeElement() should ideally do this job.

@comerc
Copy link

comerc commented May 11, 2016

$('#some-checkbox').trigger('click');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants