3

Is there a more performant way to write this.

$('#test').find('option:selected[value!=""]')
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Hussein
  • 42,480
  • 25
  • 113
  • 143

2 Answers2

5

You can tweak it at little, but using methods instead of Sizzle:

$('#test').find('option').filter(function() {
    return this.selected && this.value.length
});

Benchmark: http://jsperf.com/sizzle-vs-methods-filter/12

.filter() is about 70% faster for me.

jAndy
  • 231,737
  • 57
  • 305
  • 359
0

Well, there will always only be one selected, so you don't need a find() handler in my opinion.

I'll just write it like this:

$('#test option:selected[value!=""]')

I haven't tested it yet.

Anriëtte Myburgh
  • 13,347
  • 11
  • 51
  • 72
  • Using http://jsperf.com this is giving a 2% slower result then find. using filter is the way to go. – Hussein Feb 08 '11 at 20:29