14

What I want to do is to select all the inputs buttons on the document, except those that reside under a specific id.

Example:

<body>
<input type="button">

<div id="something">
     <input type="button">
</div>
<div id="something2">
     <input type="button">
</div>


<input type="button">
<input type="button">
<input type="button">
</body>

For example, I would like to select all the inputs, except those that resides under the <div> whose id is "something".

What I've tried:

1) $('input[type="button"]:not(:parent(#something))').addCSS();

2) $('input[type="button"] :not(#something input[type="button"])')

And other similar approaches

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Jose A
  • 10,053
  • 11
  • 75
  • 108
  • possible duplicate of [JQuery Selector for not "wrapped" elements](http://stackoverflow.com/questions/11924672/jquery-selector-for-not-wrapped-elements) – Blazemonger Feb 27 '13 at 21:12

3 Answers3

26

You can do it like this (relatively efficiently).

$("input[type=button]").filter(function() {
    return $(this).closest("#something").length == 0;
});

First, you get all the input[type=button] elements, then remove those with #something as a parent.

Another possibility is this:

$("input[type=button]").not("#something input[type=button]")

You'd have to test both to see which is more efficient, but either will work.

Working demo of both: http://jsfiddle.net/jfriend00/fjxDb/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
14

You were almost there, all you needed to do was remove the space before your :not.

$('input[type="button"]:not(#something input[type="button"])')

I know this is an old question, but it shows up first on Google, and using jquery's .not() as shown in the accepted answer is not always an option.

Niels Festjens
  • 141
  • 1
  • 4
  • Why is jQuery's `.not()` sometimes not an option? This is a jQuery question so you can always use `.not()` with jQuery, right? – jfriend00 Sep 02 '16 at 05:27
  • When you want to handle events dynamically, so also on elements that get added **after** you apply it, you'll typically use $(document).on('theEvent', 'theSelector'). There, it's not possible to combine the selector with .not(), while it is possible to use :not in the selector. – Niels Festjens Sep 03 '16 at 06:07
2

Maybe this works: $(":not(#something) input[type=button]")

Lord Spectre
  • 761
  • 1
  • 6
  • 21
  • 1
    I'd wonder about the efficiency of this since nearly every object in the document (except one) will match `:not(#something)`. – jfriend00 Aug 12 '12 at 23:39
  • Just give it a random shot... maybe you'll have some luck. Next time, I'd recommend trying it yourself if it already is this easy to reconstruct. – Kiruse Aug 12 '12 at 23:46
  • I don't think that this will affect performance very much; the final JQuery object is reduced by another selector (the `input[...]`). Anyway you can use `div:not(...)`, but it won't match the ones in the body, and you'll have to add them manually to the selector. – Lord Spectre Aug 12 '12 at 23:49