0

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?

3 Answers 3

2

Because you are appending the elements only later point in time, you are creating a jquery object before that with the selector which will fetch nothing (since the element is not yet in DOM). Instead just save the id in the array and construct the object later.

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); //just save the id
}
$('body').append(allCubes);

and

  $('#trigger').click(function() { 
      console.log(cubes[1]); 
      //create object as
      var $cube = $(cubes[1]);
  }); 

or do:

    var cubes  = []; 
    for(var i = 0; i < 150; i++) {
        var randomleft = Math.floor(Math.random()*1000),
            randomtop = Math.floor(Math.random()*1000);
        cubes.push($('<div id="cube'+i+'" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>'));
    }
    $('body').append(cubes);

and

      $('#trigger').click(function() { 
          console.log(cubes[1].attr('id')); 
       }); 

Demo

Sign up to request clarification or add additional context in comments.

3 Comments

@supersize Yes because you are creating it each time on click, if not what you can do is to get all the cubes... using a starts with selector.
thanks for your edit. But just to be clear, couldnt i do another loop with cubes.push($('#cube'+i)); after appending all cubes?
@supersize no you dont need to. See my second example and demo. It is done at one place.
0

PSL is right. alternatively just append the code everytime

randomtop = Math.floor(Math.random()*1000);
$('body').append('<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));

1 Comment

No no .. Its better to append to DOM at the end rather than during each iteration.
0

You are trying to create:

cubes.push($('#cube'+i));

before the #cubeX item is in the DOM so the jQuery object you create and put in your array has nothing in it because it couldn't find #cubeX in the DOM at the time you created your jQuery object.

I would suggest you put a common class on the added items and change your code to this:

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 + '" class="cube" style="position: absolute; border: 2px #000 solid; left: '+randomleft+'px; top: '+randomtop+'px; width: 15px; height: 15px;"></div>';
}
$('body').append(allCubes);

Then, whenever you want to get all the cube items, you can just use this:

$(".cube")

and you don't need to store them permanently in an array. Just fetch them when needed.

If you want the attribute off the second one, you can do this:

$('#trigger').click(function() { 
     console.log($(".cube").eq(1).attr('id'));
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.