views:

3135

answers:

6

I have a div (let's say the id is "container") with many elements in it, including a select element. I'd like to select all everything in the div except the select. Things I've tried:

$("#container *:not(select)")
$("#container *:not(#selectorid)")

//put a div around the select and...
$("#container *:not(#selectorcontainer)")
$("#container *:not(#selectorcontainer *)")
$("#container *:not(#selectorcontainer, #selectorcontainer *)")

Also tried without wildcard descendant selector, so just like all the above, but

$("#container:not(#selectorid)")
A: 

http://stackoverflow.com/questions/1109835/quick-html-jquery-question
Two approaches to the problem: The one with 8 votes is probably the one you are looking for.

Michael
Yep, tried that. See 2nd attempt.
Shawn J. Goff
You have the latest version of JQuery? (1.3 and above)
Michael
Just tried with 1.3. Different behavior, still not correct, but select element stays if I use #3 (from above) and other children are doing wierd things, though.
Shawn J. Goff
Need more details ...
Michael
A: 

The :not(selector) should work. But if you can't figure it out, you can select all the children, and then remove any SELECT elements afterwards. That, or manually skip those particular elements whenever you're running through your collection later.

Jonathan Sampson
+1  A: 

You can also try it using traversing methods:

$('#container').children().not('#selectorId');

or being a final option:

$('#container').children().filter(function(){
    return $(this).attr('id') !== 'selectorId';
}
Jeff
Thanks for the tip about the traversing method.I got it to work by $("#featured").children().not("select") Also I found it very strange that $("#featured").children().not("#selectorId") does not work in this case.
Shawn J. Goff
id searches may be case-sensitive (not sure).You can always use the form attributes. Form elements usually have names, so you could use: "select[name=selectname]". But getting via id is always faster.
Jeff
A: 

It is not clear what you actually want from your question. :not(select) will filter out the select from all the descendants but it will not out the select descendants. If this is your issue then try the following

Demo

$("#container *").filter( function(){ 
    return $(this).closest('select').length === 0 
})
redsquare
+9  A: 

You can simply omit the wildcard as it is optional in this case, but keep the space:

$('#container :not(select)');

Alternatively, use the .not() method to filter out the select after selecting all children:

$('#container').children().not('select');

If your problem is that children of select are still included, you could explicitly filter those out with the .not() method:

$('#container').children().not('select, option, optgroup');

or select direct children only:

$('#container > :not(select)');

You can try out jQuery selectors at the interactive jQuery selector tester for quick feedback; try for example div :not(ol) or div > :not(ol) to get a feel for what difference the direct child (>) selector makes.

Martijn Pieters
Great tip about the interactive tester! I got a couple of things to work: $("#featured > :not('select')") and $("#featured").children().not("select") buy very strangely not $("#featured").children().not("#selectorId") and not $("#featured > :not('#selectorID')")
Shawn J. Goff
Omit the quotes. Just use :not(seletor), so :not(#selectorID) and it'll work fine.
Martijn Pieters
A: 

maybe this can help you where 3 is the #select

try one of these: $("#container *:not(eq(3)) $("#container").children().filter(:not(eq(3)));

adardesign