javascript tutorial - [Solved-5 Solutions] Setting “checked” for a checkbox with jQuery - javascript - java script - javascript array
Problem:
we'd like to do something like this to tick a checkbox using jQuery:
or
Does such a thing exist?
Solution 1:
accepted jQuery 1.6+
Use the new .prop() method:
jQuery 1.5.x and below
The .prop() method is not available, so we need to use .attr().
Note that this is the approach used by jQuery's unit tests prior to version 1.6 and is preferable to using
since the latter will, if the box was initially checked, change the behaviour of a call to .reset() on any form that contains it - a subtle but probably unwelcome behaviour change.
For more context, some incomplete discussion of the changes to the handling of the checked attribute/property in the transition from 1.5.x to 1.6 can be found in the version 1.6 release notes and the Attributes vs. Properties section of the .prop() documentation.
Any version of jQuery
If you're working with just one element, we can always just modify the HTMLInputElement's .checked property:
The benefit to using the .prop() and .attr() methods instead of this is that they will operate on all matched elements.
Solution 2:
Use:
And if we want to check if a checkbox is checked or not:
Solution 3:
or
If we have custom code in the onclick event for the checkbox that we want to fire, use this one instead:
We can uncheck by removing the attribute entirely:
We can check all checkboxes like this:
Solution 4:
We can also extend the $.fn object with new methods:
Or we may want to give them more unique names like mycheck() and myuncheck() in case we use some other library that uses those names.
Solution 5:
The last one will fire the click event for the checkbox, the others will not. So if we have custom code in the onclick event for the checkbox that we want to fire, use the last one.
Related Searches to javascript tutorial - Setting “checked” for a checkbox with jQuery
jquery set checkbox checked based on valuejquery set checkbox uncheckedset checkbox checked javascripthow to check checkbox is checked or not in jqueryset checkbox checked htmljquery toggle checkboxjquery checkbox checked valuejquery checkbox eventcheck checkbox based on value javascriptselect checkbox jquery by valuejquery select checkbox by name and valuejquery check if checkbox with specific value is checkedjavascript set checkbox based on valuecheckbox checked based on namehow to checked checkbox in jquery using namejquery uncheck checkbox based on valuejavascript tutorialjava scriptjavascriptjavascript arrayjavascript booklearn javascriptjavascript codejavascript editorjavascript classjavascripjavascript debuggerjavascript onlinejavascript examplesjavascript testjavascript documentjavascript sliderwhat is javascriptjavascript form validationjavascript validatorhtml javascriptjavascript alertjavascript eventsjavascript printjavascript domjavascript objectjavascript functionhref javascriptjavascript datejavascript promptjavascript onclickjavascript returnjavascript forjavascript numberjavascript confirmjavascript onchangejavascript regular expressionjavascript ifjavascript variablejavascript timerjavascript cookiejavascript getelementbyidjavascript innerhtmljavascript calljavascript regexpjavascript includesjavascript thisjavascript eval