G
G
Gregory2018-03-14 15:39:43
JavaScript
Gregory, 2018-03-14 15:39:43

Selecting and filtering jQuery elements?

Good afternoon. Please tell me how to write the following selection correctly:

It is necessary to find all elements with the 'select' class inside the block with the 'root' class, but inside their own level, i.e. if there is another 'root' block inside the 'root' block, then its collection of elements should not be in the selection.

<div class='root'>
    <div class='select'>
    </div>
    <div class='select'>
    </div>
    <div class='select'>
    </div>
    <div class='root'>
         <div class='select'>
         </div>
         <div class='select'>
         </div>
         <div class='select'>
         </div>
    </div>
</div>


Thus, I need the console log to show 3 'select' in each 'root' block, not 6 in the 1st and 3 in the 2nd.

For convenience, a link to fidl.
https://jsfiddle.net/8grprfhb/1/

Thanks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2018-03-14
@Groyreg

$('.root').each(function(){
  console.log('---------');
  console.log($(this).children('.select'));
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question