A simple way to tell if a checkbox has been checked using jQuery is with the :checked CSS "pseudo class." Your selector should use the following syntax:
$('#edit-checkbox-id:checked')
Obviously, you need to replace checkbox-id with the actual name of the form element. This will attempt to grab any checked checkbox with that ID. If none are found, the result set will be null. So, combining this with jQuery's .val() method, you can use the following conditional statement to execute code if a checkbox has been checked:
if ($('#edit-checkbox-id:checked').val() !== undefined) {
// Insert code here.
}
(Note: the checkboxes element in the Forms API produces unique IDs for each option. View the source to see what I mean.)
Comments
The example can be simplified
If you want to only do something to elements that are selected as in this case (here we want to do something if the checkbox is checked) you can simplify the code to
and the function will only run if the checkbox with the selected id is checked.
If you use a selector that can return more than one element, the function is applied each of the matching elements (one at a time), so
will run the function for each checkbox that is checked.
For jQuery 1.3
Liked your second method that iterates though checked checkboxes. For those who are using drupal 7 with jQuery 1.3 the second method will look like this:
I had hard time with that code not working and then came accros this article on checking if checkbox is checked.
To make it check every time you uncheck or check the box.... ;)
Replace #edit-submitted-ik-heb-een-vraag-of-opmerking-ik-heb-een-vraag-of-opmerking-check-yes with your element id
Another way
I couldn't get the method
I couldn't get the method you mentioned to work in IE. The following jquery string will return a boolean in both firefox and IE:
I believe this is the most
I believe this is the most correct solution and is understandably the most readable solution.
Another method
Another method is to retrieve the "checked" attribute:
jQuery 1.2.x
Has something changed since jQuery 1.2.x?
I've tried:
and none of them work when I check '#edit-checkbox-id' ON.
jQuery 1.2.x
I think you now need to use the following syntax, at least this works for me.
is(':checked')
I have jQuery 1.5.x and this works perfectly. 8-)
Also, to get the changing flag, using the .change() event works great.
$('#edit-checkbox-id').change(my_tracking_function);
I had to use !=, rather than
I had to use !=, rather than !==.
Ya Not Identical Operator
Ya Not Identical Operator doesn't work at all because the return value is string "on". Has to use not equal operator.
The best solution! thx
The best solution!
thx
value is 'undefined' if is
value is 'undefined' if is not checked.
so...
if ($('#checkbox-id:checked').val() !== undefined) {
// stuff here
}
How to do it in php?
As per subject
My Drupal sites:
University New Student Form
Hey, I'm using if statements for checkboxes placed after e-mail addresses to send a copy of the form to the father personal, mother personal, father business, mother business, etc. However, since it passes to a hidden input field, it will only send to one address per submission. I was thinking that building an array of addresses passed as a string separated by commas would work, but am not sure how to implement that.
An example of the IFs I have been using below:
Suggestions?