Skip to content

jQuery 1.3.1 Selector Bug and Workaround

In jQuery 1.2.6, if you wanted to select all inputs with empty values, you could do this:

var emptyInput = $("form input[value='']");

That’s a little hard to read, so note that the quotes around the entire selector are double quotes, but the quotes inside the square brackets are two single quotes. This says, "Give me any input where the value is equal to empty string."

This is broken in jQuery 1.3.1. Try it, and you will see (if you have a debugger attached):

Syntax error, unrecognized expression: value=""]

A fix for the bug has been checked in, so you can get a corrected version in the nightly build. If you prefer to stick with 1.3.1, I’ve found a workaround; do this instead:

var emptyInput = $("form input:not([value])");

This works because empty string is implicitly converted to false in JavaScript.

Update: The bug is fixed in jQuery 1.3.2.

{ 2 } Comments

  1. Marc Rohloff | February 17, 2009 at 7:37 am | Permalink

    If you are using the JQuery validator you could use the custom selector it provides: $(’form input:blank’)

  2. Craig Stuntz | February 17, 2009 at 7:54 am | Permalink

    Thanks, Marc; that’s a good tip.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 713 access attempts in the last 7 days.

Close