I want to do something like this
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
I wish to set the value.
Is such a thing built into jQuery?
I want to do something like this
or
I wish to set the value. Is such a thing built into jQuery? |
|||||||||||
|
It certainly is in jQuery 1.6+Use the new
jQuery 1.5 and belowThe To check the checkbox (by setting the value of the checked attribute) do
and for un-checking (by removing the attribute entirely) do
Any version of jQueryIf you're working with just one element, it will always be fastest to use
|
|||||||||||||||||||||
|
You can do
or
If you have custom code in the onclick event for the checkbox that you want to fire, use this one instead:
You can uncheck by removing the attribute entirely:
You can check all checkboxes like this:
|
|||||||
|
and if you want to check if a checkbox is checked or not:
|
|||||||||||||||||
|
The last one will fire the click event for the checkbox, the others will not. So if you have custom code in the onclick event for the checkbox that you want to fire, use the last one. |
|||||||||||||||||
|
You can also extend the $.fn object with new methods:
Then you can just do:
Or you may want to give them more unique names like mycheck() and myuncheck() in case you use some other library that uses those names. |
|||||
|
This selects elements that have the specified attribute with a value containing the given substring:
It will select all elements that contain ckbItem in its name attribute. |
||||
|
To check a checkbox you should use
or
and to uncheck a check box you should always set it to false:
If you do
it removes the attribute all together and therefore you will not be able to reset the form. BAD DEMO jQuery 1.6. I think this is broken. For 1.6 I am going to make a new post on that. NEW WORKING DEMO jQuery 1.5.2 works in Chrome. Both demos use
|
|||||||||||||||
|
This is the correct way of checking and unchecking checkboxes with jQuery, as it is cross-platform standard, and will allow form reposts.
By doing this, you are using JavaScript standards for checking and unchecking checkboxes, so any browser that properly implements the "checked" property of the checkbox element will run this code flawlessly. This should be all major browsers, but I am unable to test previous to Internet Explorer 9. The Problem (jQuery 1.6): Once a user clicks on a checkbox, that checkbox stops responding to the "checked" attribute changes. Here is an example of the checkbox attribute failing to do the job after someone has clicked the checkbox (this happens in Chrome). http://jsfiddle.net/xixonia/zgcrB/ The Solution: By using JavaScript's "checked" property on the DOM elements, we are able to solve the problem directly, instead of trying to manipulate the DOM into doing what we want it to do. http://jsfiddle.net/xixonia/WnbNC/ This plugin will alter the checked property of any elements selected by jQuery, and successfully check and uncheck checkboxes under all circumstances. So, while this may seem like an over-bearing solution, it will make your site's user experience better, and help prevent user frustration.
Alternatively, if you do not want to use a plugin, you can use the following code snippets:
|
|||||||||||||||||||||
|
We can use
We can use this for all jQuery versions without any error. |
||||
|
Here is code for checked and unchecked with a button:
|
||||
|
I couldn't get it working using:
Both true and false would check the checkbox. What worked for me was:
|
|||||||||||
|
Assuming that the question is "How do I check a checkbox-set BY VALUE?"... Remember that in a checkbox set all input tags have the same name, they differ by value, and you can not (neither elegantly or usefully) use a ID for each one. Xian's answer can be extended with a more specific selector, using the following line of code:
|
||||
|
If you are using PhoneGap doing application development, and you have a value on the button that you want to show instantly, remember to do this
I found that without the span, the interface will not update no matter what you do. |
||||
|
I'm missing the solution I'll always use:
|
||||
|
Try this:
|
||||
|
Here is a way to do it without jQuery - live sample: JavaScript:
HTML:
|
||||
|
If using mobile and you want the interface to update and show the checkbox as unchecked, use the following:
|
|||
|
Here's the complete answer using jQuery I test it and it works 100% :D
|
|||
|
To check a checkbox using jQuery 1.9 just do this:
To uncheck, use:
Here' s what I like to use to toggle a checkbox using jQuery:
|
|||
|
Be aware of memory leaks in IE prior to IE9, as the jQuery documentation states:
|
|||
|
In jquery,
or
In Javascript,
|
|||
|
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.