jQuery : find empty select boxes (or selects with a specific value)

Another super short jQuery solution to a super long late-night quest on my part.

I have a form with a variable number of select boxes. Before the form can be submitted, I need to verify that there are no selections left unchanged.

The select boxes start out with a default value of ''  (empty quotes). To search for them , i was using all sorts of variations on jquery 'val()', but that is more useful for setting a value than finding a select with a value. After a few various connotations... here's the one that works:

 var valCt = $('select.colorselect[value=""]').size();

This finds all selects with the class of 'colorselect' and the value of '', and returns the size, which in jQuery talk means 'how many'. If I have 3 unchanged select boxes, valCt will equal '3'.

From there it is an easy hop to add a check to the submit action of my form, checking to make sure valCt is 0.

if (!(valCt == 0)) {
return false
} else [ ... submit form here ... ]

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Don't you just love it, that seemingly difficult/tricky problems like this one can have the easiest solution with jQuery. I had a similar problem with using grouped checkboxes to have them check/uncheck with a parent checkbox. The easiest solution in jquery:
$("input.parent").click(function() { $("input:checkbox", $(this) ).attr("checked", $(this).checked ); });

I just love jQuery!!
# Author Stefan | 2/12/09 4:31 AM
Yes i am continually amazed by these short, compact lines of code that can do so much.

one question on your example - what does the second part of the selector do, after the comma?

i.e. what does this line do:
$("input:checkbox", $(this) )
as opposed to simply:
$("input:checkbox")
?

I know I have seen that syntax before but didn't retain the meaning... too much to learn all the time!
# Author Michael Evangelista | 2/12/09 12:40 PM
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista