0

The following code alerts: "object Object"

var shipImgs = $("#div").children();
alert(shipImgs);

div contains 4 children (image tags).

<div id="div">
    <img src="/imgs/spaceship_still.jpg" alt="space"/>
    <img src="/imgs/spaceship_laser.jpg" alt="space"/>
    <img src="/imgs/spaceship_motion.jpg" alt="space"/>
    <img src="/imgs/spaceship_destroyed.jpg" alt="space"/>
</div> 

I'd like each of these images to be added to an array. But before I can even do that, I need to know how to handle shipImgs... How can I access that object and get the html to push to the array?

0

2 Answers 2

2

if the div has more than one children then $("#div").children will already return you an array

http://jsfiddle.net/jBVZU/


or do you want to get an array of img src??

var shipImgs = $("#div").children();
console.log(shipImgs);

var imgArr=[];

$.each(shipImgs,function(i,elem){
imgArr.push($(elem).attr("src"));
});
console.log(imgArr);

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

in simple terms, jQuery objects are just arrays with "decorative methods"
Thanks @3nigma, the second part was exactly what I was looking for!
0

Try this. It will store the actual html string of the image in an array.

var imageArray = [],
shipImgs = $("#div").children();

shipImgs.each(function () {
    var imghtml = $(this);

    imageArray.push(imghtml.wrap('<div />').parent().html());
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.