3

I'm trying to select all inputs except the inputs inside some div.class. I do not know why it does not work correctly. My structure looks something like below. And why selector :not not work. And what can I do to exclude all inputs from the "exclude" div. Because i want only select inputs: i1,i2.

console.log($("div.exclude input").length);
console.log($("div:not(.exclude) input").length);
console.log($("div input").length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
 <div id="tab1">
  <div>
   <div>
    <input id="i1"/>
   </div>
   <input id="i2" />
  </div>
 </div>

<div id="tab2" class="exclude">
  <div>
   <div>
    <input  id="i3" />
   </div>
    <input  id="i4" />
  </div>
 </div>
</div>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
MiiiM
  • 33
  • 5

5 Answers5

2
$('input').not('.exclude *')

or

$('input:not(.exclude *)')

These will grab all inputs which are not descendants of elements with the exclude class. You can get more specific on which inputs (maybe only the ones under a certain div or class) but this should get you the exclusion you're looking for.

You can see a working example here

Pabs123
  • 3,385
  • 13
  • 29
1

In your HTML structure:

<div id="tab2" class="exclude">
  <div>
   <div>
    <input  id="i3" />
   </div>
    <input  id="i4" />
  </div>
 </div>
</div>

each <input> is inside at least one <div> that does not have the class "exclude". Therefore your selector is working but it's not getting the result you want.

Instead, qualify the inputs selected the simple way:

console.log($("div input:not(.exclude *)").length);)

That selector will first select all of the <input> elements (well the ones inside <div> elements), but then exclude all of <input> elements that have an element with class "exclude" somewhere above them in the DOM.

If the original qualifier of being inside some <div> isn't really important, then all you need is "input:not(.exclude *)".

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

I marked inside your HTML the div that is not with the exclude class, and this is why you got 4 inputs on your console.log($("div:not(.exclude) input").length);

You can filter out the inputs that have parents with the exclude class:

console.log($("div.exclude input").length);
console.log($("div:not(.exclude) input").length);
console.log($("div input").length);

console.log($("input").filter(function() { return $(this).parents('.exclude').length > 0}).length);

$("input").filter(function() { return $(this).parents('.exclude').length > 0}).css('background', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
 <div id="tab1">
  <div>
   <div> 
    <input id="i1"/>
   </div>
   <input id="i2" />
  </div>
 </div>

<div id="tab2" class="exclude">
  <div>
   <div> <!-- This div is not with the class explude -->
    <input  id="i3" />
   </div>
    <input  id="i4" />
  </div>
 </div>
</div>
Dekel
  • 60,707
  • 10
  • 101
  • 129
0

Problem: there are inner divs which does not have the class and they satisfy the selector too.

Solution: Add another class to the divs which you want to be part of your selector and then use this new class too .. Like

<div id="tab1" class="tabs"> And

<div id="tab2" class="tabs exclude">

And then change script to

$("div.tabs:not(.exclude) input")

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
0

Take this:

console.log($("div.table.exclude input").length);
console.log($("div.table:not(.exclude) input").length);
console.log($("div input").length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  
  <div id="tab1" class="table">
    <div>
      <div>
        <input id="i1"/>
      </div>
      <input id="i2" />
    </div>
  </div>

  <div id="tab2" class="table exclude">
    <div>
      <div>
        <input  id="i3" />
      </div>
      <input  id="i4" />
    </div>
  </div>
  
</div>
num8er
  • 18,604
  • 3
  • 43
  • 57