Possible defect with jQuery ':checked' selector (finding checked checkbox when there are none)
Posted
by Mphammad Ashour
on Stack Overflow
See other posts from Stack Overflow
or by Mphammad Ashour
Published on 2010-05-09T13:56:34Z
Indexed on
2010/05/09
13:58 UTC
Read the original article
Hit count: 327
jQuery
|JavaScript
Found a strange thing. The following code is supposed to set a default value for a UI element based on whether one of a group of checkboxes (all having attribute name="privacy") is checked (in say, a form postback). If there is no value in the HTML sent by the server (the form is not a postback), the default value should be firstMenuItemKey. Otherwise, it should be the one checked in the underlying HTML.
However, while this works on the first call to the URL that includes the form, a refresh of the page will set the value of defaultPrivacyValue to one of the privacy checkboxes'. I've checked that the server isn't checking the checkbox before the script runs (it's not). I've also checked whether somewhere else in the script is checking the checkbox (it's not).
var defaultPrivacyValue = firstMenuItemKey;
$('input[name=privacy]:checked').each(function() {
defaultPrivacyValue = $(this).next().find('.name').text();
});
selectMenuItem(defaultPrivacyValue);
When I chaged the selector to 'input[name=privacy][checked=checked]' it worked as expected. Is this a defect in the jQuery implementation of DOM retrieval based on the ':checked' selector???? I'm using jQuery 1.4.2.
Thanks for any help you can give.
© Stack Overflow or respective owner