I am working on a script that will push each child element into a global array (for processing later on in my script) but for some reason its not actually pushing the element into the array.
Code:
var childElements=new Array();
function getChildren(elem){
$(elem).children().each(function(index, value){
childElements[index] = $(this);
});
}
Am I doing something wrong?
Since a jQuery object is an Array-like object, I'd probably just use that instead of creating an Array of individually wrapped objects.
var childElements=$(elem).children();
If you intend to add more elements, you can .push() always .add() new elements. This will also make sure you don't have duplicates.
var childElements= $();
function getChildren(elem){
childElements = childElements.add( $(elem).children() );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With