class selector refuses after append to body

Posted by supersize on Stack Overflow See other posts from Stack Overflow or by supersize
Published on 2013-10-28T21:51:55Z Indexed on 2013/10/28 21:53 UTC
Read the original article Hit count: 175

Filed under:
|
|

I'm appending loads of divs in a wrapper:

    var cubes    = [], 
    allCubes  = '',
    for(var i = 0; i < 380; i++) {
    var randomleft = Math.floor(Math.random()*Math.floor(Math.random()*1000)),
        randomtop = Math.floor(Math.random()*Math.floor(Math.random()*1000));
    allCubes += '<div id="cube'+i+'" class="cube" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 9px; height: 9px; z-index: -1"></div>';
}  

$('#wrapper').append(allCubes); // performance

for(var i = 0; i < 380; i++) {
 cubes.push($('#cube'+i));  
}

and then I would like to make them all draggable with jQueryUI and log their current position.

var allc = $('.cube');
    allc.draggable().on('mouseup', function(i) {
    allc.each(function() {
    var nleft = $(this).offset().left;   
    var ntop = $(this).offset().top;
    console.log('cubes['+i+'].animate({ left:'+nleft+',top:'+ntop+'})');
  });
});

Unfortunenately it does not work. They are neither draggable nor there comes up a log.

Thanks

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery