homemrobo
homemrobo

Reputation: 615

Jquery each interation problem

I got this random position script. But it works only on the first image... What I'm doing wrong?

var randnumsX = [1,2,3,4,5,6,7,8];
var randnumsY = [1,2,3,4,5,6];

$('#obra img').each(function(i,el) {

    m = Math.floor(Math.random()*randnumsX.length);
    randnumsX = randnumsX.splice(m,1);
    posx = Math.floor(m * 50);

    n = Math.floor(Math.random()*randnumsY.length);
    randnumsY = randnumsY.splice(n,1);
    posy = Math.floor(n * 50);

    $(el).css({position:'absolute', left: posx + 155, top: posy});      
    $(el).fadeIn('slow');

}); 

Upvotes: 1

Views: 131

Answers (2)

jbrond
jbrond

Reputation: 727

splice returns the removed element not the array with the element removed.

Upvotes: 1

Meenakshi
Meenakshi

Reputation: 121

If you are accessing the div then you won't need # sign

$('div img').each(function(i,el) {

m = Math.floor(Math.random()*randnumsX.length);
randnumsX = randnumsX.splice(m,1);
posx = Math.floor(m * 50);

n = Math.floor(Math.random()*randnumsY.length);
randnumsY = randnumsY.splice(n,1);
posy = Math.floor(n * 50);

$(el).css({position:'absolute', left: posx + 155, top: posy});      
$(el).fadeIn('slow');

});

Upvotes: 0

Related Questions