Oliver Nassar

I can be reached at onassar@gmail.com.

For my open source work, check out github.com/onassar


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

View more posts

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.