3

Is there a more performant way to write this.

$('#test').find('option:selected[value!=""]')
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Hussein
  • 41,814
  • 25
  • 111
  • 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
  • 223,102
  • 54
  • 301
  • 354
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
  • 12,777
  • 11
  • 48
  • 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