Oliver Nassar

jQuery('[attribute="value"]') vs. jQuery('*:checked')

July 27, 2011

Using jQuery a lot for a recent project, I found my way around a selector I was trying to run. MooTools being my library of choice, I would expected to run a selector for checked radio inputs as follows:

var checked = $('id').getElements('input[checked=checked]');

Running the equivalent in a jQuery environment would be written as:

var checked = $('#id input[checked=checked]');

However, in jQuery (and maybe even MooTools, but I don't think so), running the above will only, and always, return you the radio-box inputs that were loaded initially as part of the server-transmitted source. Selecting a different radio input in the same family/name-group, and then running the same above command, would result in the exact same set being returned. I'm not sure if this is a caching issue by jQuery or a bug.

The way around this, I would find, would be to issue the following command:

var checked = $('#id input:checked');

Simple enough, but not what I'd expected. In order to get the negation of this, the following worked wonderfully well:

var unchecked = $('#id input:not(:checked)');

Hope that sheds some light on jQuery's selector syntax for attribute values.