I'd like to do something like this to tick a checkbox
using jQuery:
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
Does such a thing exist?
I'd like to do something like this to tick a
or
Does such a thing exist? |
||||
jQuery 1.6+Use the new
jQuery 1.5.x and belowThe
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 For more context, some incomplete discussion of the changes to the handling of the Any version of jQueryIf you're working with just one element, you can always just modify the
The benefit to using the |
|||||||||||||||||||||
|
Use:
And if you want to check if a checkbox is checked or not:
|
|||||||||||||||||||||
|
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). 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. 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:
|
|||||||||||||||||||||
|
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:
|
|||||||||||||||||
|
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. |
|||||||||
|
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. |
|||||||||
|
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
|
|||||||||||||||||||||
|
I'm missing the solution. I'll always use:
|
|||||
|
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. |
||||
|
Assuming that the question is... How do I check a checkbox-set BY VALUE?Remember that in a typical checkbox set, all input tags have the same name, they differ by the attribute Xian's answer can be extended with a more specific selector, using the following line of code:
|
||||
|
Here is a way to do it without jQuery
|
||||
|
Try this:
|
||||
|
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:
|
|||||
|
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. |
||||
|
We can use
We can use this for all jQuery versions without any error. |
|||||
|
Here is code for checked and unchecked with a button:
|
|||||
|
Another possible solution:
|
||||
|
Here is the code and demo for how to check multiple check boxes... http://jsfiddle.net/tamilmani/z8TTt/
|
|||||
|
If using mobile and you want the interface to update and show the checkbox as unchecked, use the following:
|
|||||
|
To check and uncheck
|
|||||
|
Be aware of memory leaks in Internet Explorer prior to Internet Explorer 9, as the jQuery documentation states:
|
|||||||||||||
|
|
|||||
|
For jQuery 1.6+
For jQuery 1.5.x and below
To check,
|
|||
|
This is probably the shortest and easiest solution:
or
Even shorter would be:
Here is a jsFiddle as well. |
||||
|
I couldn't get it working using:
Both true and false would check the checkbox. What worked for me was:
|
|||||||||||||||||||||
|
When you checked a checkbox like;
it might not be enough. You should also call the function below;
Especially when you removed the checkbox checked attribute. |
|||
|
Plain JavaScript is very simple and much less overhead:
|
|||||
|
Here's the complete answer using jQuery I test it and it works 100% :D
|
|||
|
In jQuery,
or
In JavaScript,
|
|||||||||
|
In case you use ASP.NET MVC, generate many checkboxes and later have to select/unselect all using JavaScript you can do the following. HTML
JavaScript
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
$("#mycheckbox").click();
– HumanInDisguise May 11 '15 at 14:06