0

Possible Duplicate:
jQuery OR Selector?

I would like to for each select in the matched set find the first option that either has a class placeholder or an empty value something along the lines of

$(selectorOrJquery).find('select').andSelf().filter('select')
  .find('(option.placeholder OR option[value=""]):first)

That is unfortunately not a valid sizzle (or CSS3) syntax - anyone know if there is a valid way to do this with selectors?

Obviously I could write procedural code to find this for me, but I'm hoping for something a bit more slick

Edit: To clarify, I do not think a comma simply works. Imagine the following

<select>
  <option class="placeholder" value="0">Select Something</option>
  <option value="">Legitimate option that gets handled in JS</value>
</select>

In this case I want only the first selected, NOT both

Community
  • 1
  • 1
George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • I answered this way back when. – Daniel A. White Sep 28 '11 at 15:42
  • Oh boy. I simply can't decide who has the most valid answer here :) – jwegner Sep 28 '11 at 15:45
  • @DanielA.White Would that really preserve the order of prescendence of operations though? What if a single select contains a .placeholder followed by a [value=""] - I believe comma syntax will select both and I wont only the first matched to either – George Mauer Sep 28 '11 at 15:45

4 Answers4

4

I have also run into an issue like this in the past and settled for something like this.

$(selectorOrJquery).find('select').andSelf().filter('select')
  .find('option.placeholder, option[value=""]').eq(0);
John Kalberer
  • 5,690
  • 1
  • 23
  • 27
1

This will return the first option of a select that is either empty or has a class placeholder.

$("select option:first").filter(":empty,.placeholder")
amosrivera
  • 26,114
  • 9
  • 67
  • 76
1

Just use a comma, which is a valid CSS/jQuery Selector.

$(selectorOrJquery).find('select').andSelf().filter('select')
  .find('(option.placeholder,  option[value=""]):first)
jwegner
  • 7,043
  • 8
  • 34
  • 56
1

the css OR is the comma, so the selector you want would be

'option.placeholder:first, option[value=""]:first'
Einacio
  • 3,502
  • 1
  • 17
  • 21