How to get only the first level divins?

I have a div with id content , and I want to get the id elements of the first level div , for example. box1 , box2 , box3 . How can I do that?

 <div id="content"> <div id="box1" class="box1class"> <div>...</div> </div> <div id="box2" class="box1class"> <div>...</div> </div> <div id="box3" class="box1class"> <div>...</div> </div> </div> 
+6
source share
3 answers

Use the child filter > .

 var ids = []; $("#content > div").each(function() { ids.push(this.id); }); 

This can be shortened using map() :

 var ids = $("#content > div").map(function() { return this.id; }).get(); 
+6
source
 $("#content > div") 

Like it. You can get a div array like this

  var array = $("#content > div").map(function(){ return this.id; }).get(); 

See in jsfiddle http://jsfiddle.net/DsyzV/

+5
source

Using:

 $("#content > div").each(function() { var divId = this.id; }); 
+1
source

Source: https://habr.com/ru/post/912839/


All Articles