PabloEscalar
PabloEscalar

Reputation: 3

JQuery append images in a for loop

I am trying to append the same image multiple times to a div element inside a for loop. However, the output I'm getting is just "[object HTMLImageElement]". The stuffDisplayed variable is initialized to 48 at the top of the file.

var stuff = new Image();
stuff.src = "images/stuff.png";

function createStuff()
    {
        var stuffPerRow = 8;
        var numRows = stuffDisplayed / stuffPerRow;
        
        for (var i = 0; i < numRows; i++)
        {
            $("#stuff").append('<div id="row' + i + '"></div>');
            
            for (var j = 0; j < stuffPerRow; j++)
            {
                $("#row" + i).append("<a src=" + stuff.src + " href=#>" + stuff + "</a>");   
            }
        }
    }

Upvotes: 0

Views: 39

Answers (1)

Barmar
Barmar

Reputation: 781028

You can't concatenate a DOM element to a string. Just use the <img> HTML.

var stuff = '<img src="images/stuff.png">';

Upvotes: 1

Related Questions