YUI Selector Utility: CSS3 Attribute Selectors and Pseudo-classes
Saturday, March 28th, 2009I have been using the YUI Selector Utility for a few months, but I just recently discovered how powerful it really is. I have been converting my imValidateForm JQuery plugin into a YUI widget. One of the validations this widget checks is whether a set of radio buttons are checked. I originally created validation object with pure Javascript. To determine whether the radio buttons were checked, I created the following code:
var flds = document.getElementsByName(‘gender’); var nL = flds.length; for (var j=0; j<nL; j++) { if (flds[j].checked) { ischecked = true; break; } } |
This is so verbose when compared to the YUI implementation of the same code:
var ischecked = (YAHOO.util.Selector.query('input[name=gender]:checked').length > 0) ? true : false; |
Not only am I able to use the CSS3 Attribute Selector and Pseudo-classes, I can combine the two to create more succinct code. Marvelous.



March 28th, 2009 at 7:41 pm
I think the ? true : false; part isn’t really needed, since the expression in parentheses is boolean, and will return true or false anyway. You can reduce it to this:
var ischecked = (YAHOO.util.Selector.query(’input[name=gender]:checked’).length > 0);
March 28th, 2009 at 8:19 pm
Good to know. Thanks for the pebble. I guess we are all grasshoppers