how do i get the child divs and put it in a array?
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
</div>
var array = $('#parent > div').... (im stuck).
thanks!
how do i get the child divs and put it in a array?
<div id="parent">
<div id="child1"></div>
<div id="child2"></div>
</div>
var array = $('#parent > div').... (im stuck).
thanks!
var divs = $('#parent > div')
for(i = 0;i < divs.length; i++)
{
...
}
Actually there is nothing wrong with your jQuery code. You should wrap it into the .ready() callback:
$(function(){
var arr = $('#parent > div');
});
If you "really" want a a plain array you need to use .get()
$(function(){
var arr = $('#parent > div').get();
});
For the array of the ID's, as you mentioned in your comment use:
var arr = $('#parent > div').map(function(){
return this.id;
}).get();
If you just wan't the id attributes as an array you can try this:
var ids = $.map($('#parent > div'), function(child) { return child.id; });
It utilizes the jQuery.map function which transforms one array to another.