I'm creating loads of random divs and append them into the body:
var cubes = [],
allCubes = ''
for(var i = 0; i < 150; i++) {
var randomleft = Math.floor(Math.random()*1000),
randomtop = Math.floor(Math.random()*1000);
allCubes += '<div id="cube'+i+'" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>';
cubes.push($('#cube'+i));
}
$('body').append(allCubes);
later then I want to select a specific array element (which are jquery objects as seen above) in a click handler:
$('#trigger').click(function() {
console.log(cubes[1].attr('id'));
});
and I throws me undefined. Why?