views:

61

answers:

2

Hi everyone! I have a stack with my work today. My stack here:

I have a select html element and it has MULTIPLE mode:

<select class="test" MULTIPLE></select>

(MULTIPLE mode also type as : multiple="multiple" i inclue here)

<select class="test" multiple='multiple'></select>

now i only want select this element in many normal select element:

<select class="test" ></select>
<select class="test" ></select>
<select class="test" multiple='multiple'></select>
<select class="test"> </select>

i was using jQ like this:

$(".test[!MULTIPLE]").css('border','solid 1px red');

but all select element has border red;

How can i get only select element MULTIPLE. And get select not MULTIPLE mode?

+5  A: 

Try this:

$(".test:not([multiple])").css('border','solid 1px red');

Edit: As Reigel mentions, you can get the same result set with better performance if you avoid the jQuery pseudo-selector:

$(".test").not([multiple]).css('border','solid 1px red');

Edit 2: As you can tell from the comments, some quick testing shows that, at least for a few of us, the second option is actually slower. Digging further, according to css3.info, native support for the :not CSS3 selector is more widespread than I thought. That probably makes all the difference (sorry IE7), assuming jQuery uses the native selector when available.

Edit 3: Further thanks to @nickf, who ran these tests in IE8, and found no substantive difference between the two. In light of all this ambiguity, it would be wise to test in your target browser if jQuery pseudo-selectors, or :not/.not specifically, fall into a code hot spot that has a material impact on performance (and if you have a controlled target browser environment).

But if your target is all browsers, it looks like the best advice is to use what best fits your code, and avoid premature optimization.

Ken Redler
`:not()` selector slower than `.not()` ;)
Reigel
Good point. Editing to show the alternative.
Ken Redler
@Reigel: really? I just benchmarked the two. `:not` 1235ms, `.not` 6695ms
nickf
Here's the code I ran, if you're interested:`console.time(':not'); for(var i = 0; i < 1000; i++) jQuery('*:not(li)'); console.timeEnd(':not');``console.time('.not'); for(var i = 0; i < 1000; i++) jQuery('*').not('li'); console.timeEnd('.not');`
nickf
That's interesting. Maybe the browser you're using has a native (CSS3) `:not` implementation?
Ken Redler
Just ran your test in Safari 4.04 (Mac): `:not` 23ms, `.not` 126ms.
Ken Redler
ya. Thankyou for your answer :). i was done it !.
Rueta
@Ken: I just ran the tests on IE8 and the result was close enough to identical (+/- 1%). I would edit your answer to not say that `.not` is faster, since .. well, it isn't.
nickf
@nick: Editing to add your input. Everybody loves ambiguity. Seems like sometimes it's faster and sometimes not (Firefox 3.6 shows :not to be about four to five times faster)...
Ken Redler
+2  A: 
$('.test').not('[multiple]').css('border','solid 1px red');

but if you are using IE 7 and below, borders in select elements won't change as expected..

Reigel