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)")
Source: Tips4all
You can simply omit the wildcard as it is optional in this case, but keep the space:
ReplyDelete$('#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.
You can also try it using traversing methods:
ReplyDelete$('#container').children().not('#selectorId');
or being a final option:
$('#container').children().filter(function(){
return $(this).attr('id') !== 'selectorId';
}
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.
ReplyDeleteIt is not clear what you actually want from your question.
ReplyDelete: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
})
http://stackoverflow.com/questions/1109835/quick-html-jquery-question
ReplyDeleteTwo approaches to the problem: The one with 8 votes is probably the one you are looking for.
maybe this can help you
ReplyDeletewhere 3 is the #select
try one of these:
$("#container *:not(eq(3))
$("#container").children().filter(:not(eq(3)));
$(#container select).siblings()
ReplyDeletehttp://docs.jquery.com/Traversing/siblings