Skip to content Skip to sidebar Skip to footer

Mystery Elements Appearing When Appending

I have the following PHP and JS:

Solution 1:

You are using for...in to loop over an array, which is not recommended. As a result, you're probably picking up a bunch of properties you didn't mean to loop over and are getting garbage appends as a result.

Use a regular for loop (for (var i = 0; i < array.length; i++)) instead and it should work as expected.


Solution 2:

The for in ... is creating the issue, as it is iterating over the properties of the Array object as well.

Change the for loop to:

$.each(window.depthElems, function(i, a){
    $('body').append('<img src="' + a[0] +
                         '" style="margin-left: ' + a[1] + 
                         'px; top: ' + a[2] + 
                         'px;" data-velocity="' + a[3] +
                         '" class="depthElem" />'); 
});

Post a Comment for "Mystery Elements Appearing When Appending"