Tomcatom
Tomcatom

Reputation: 365

Finding a list of elements in Javascript

I've created a function that creates a new img and puts it in every tag which has the css class "painted":

function putPiece(){
    var painted = document.querySelectorAll('.painted');

for (var i = 0, len = painted.length; i < len; i++) {
    var a = i+1
    var squareID = painted[i].id;
    painted[i].taken = 'BP'+a;

    var img = document.createElement("img");
    painted[i].appendChild(img);
    img.id = "BP"+a; //unique ID for every img
    img.src = "blackP.png"; //the image to use
    img.pstn = squareID; //the ID of the <td> it's in


}
}

Iv'e given properties to each object, as noted above. Now I would like to create an array that would contain the "img.pstn" of all the "img" objects. Any ideas? thanks ahead!

Upvotes: 0

Views: 63

Answers (1)

chinabuffet
chinabuffet

Reputation: 5588

Create an empty array outside the loop, and at the bottom of the loop just add:

arr.push(squareID);

Upvotes: 4

Related Questions