2

I need to have a complicated selection in jQuery it should be as follows:

var ctrls = $('.ctrl').not('.ctrl:disabled,.ctrl:parent:hidden');
ctrls.parents('.InsideContainerControl:hidden').remove();

The thing is obviously that the second line actually removes the element from the dom while what i am looking for is a way to remove it from the selection list

j08691
  • 204,283
  • 31
  • 260
  • 272
user1322801
  • 839
  • 1
  • 12
  • 27

1 Answers1

2

Something like this should work, you first filter out the parents, then filter out the children:

$('.InsideContainerControl')                   // get all parents
.not('.InsideContainerControl:hidden')         // remove hidden ones
    .find('.ctrl')                             // get all children from the remaining
    .not('.ctrl:disabled,.ctrl:parent:hidden') // remove disabled ones

Watch this, should ignore 1, 2 (parent hidden) and 4 (disabled):

var sel = 
    $('.InsideContainerControl')                   // get all parents
    .not('.InsideContainerControl:hidden')         // remove hidden ones
        .find('.ctrl')                             // get all children from the remaining
        .not('.ctrl:disabled,.ctrl:parent:hidden');// remove disabled ones

alert(sel.text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="InsideContainerControl" style="display: none">
  <button class="ctrl">1</button>
  <button class="ctrl" disabled>2</button>
</div>
<div class="InsideContainerControl">
  <button class="ctrl">3</button>
  <button class="ctrl" disabled>4</button>
</div>
Shomz
  • 37,421
  • 4
  • 57
  • 85