0

I have a for loop nested in a for loop, where I'm trying to append some dynamically created elements, and I keep getting the error: 'Uncaught TypeError: undefined is not a function', and I'm not sure why. Here's what I have:

HTML:

<div class="particle"></div>

JS:

number_of_particles = 30 + Math.floor(Math.random() * 40);
number_of_layers =  3 + Math.floor(Math.random() * 5);
$particle = $(".particle");

for ( var i = 0; i < number_of_layers; i++ ) {
    var paralax_speed, $particle_layer, $particle_layer_particle;

    paralax_speed = Math.random().toFixed(2);
    $particle_layer = "<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>";
    for ( var i = 0; i < number_of_particles; i++ ) {
        $particle_layer_particle = "<div class='particle'></div>";
        $particle_layer.append($particle_layer_particle);
    }
    $particle.append($particle_layer);
}
6
  • 1
    $particle_layer = $("<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>"); should do it Commented Dec 28, 2014 at 18:53
  • 3
    Note how both loops use i <- bad idea ! Commented Dec 28, 2014 at 18:53
  • micheal that worked, thank you, if you add it as an answer I'll accept. Just makes a Jquery object right? Commented Dec 28, 2014 at 18:55
  • adeneo what's another good variable name instead of i? Commented Dec 28, 2014 at 18:55
  • 2
    Uhm, j .. no wait . .. k , or maybe l ... ouch, can't decide ! Commented Dec 28, 2014 at 18:57

1 Answer 1

2

You need a jQuery object:

$particle_layer = $("<div class='particle-layer' data-stellar-ratio='" + paralax_speed + "'></div>");

And two for-loops:

var i, j;
for (i = ...)
    for (j = ...)
Sign up to request clarification or add additional context in comments.

3 Comments

one more question, these are getting a style of display:none put on them for some reason, any idea why that's happening?
@loriensleafs Ask new question, this is completly unrelevant to opened question. And don't forget to mark this answer as accepted one (and you can still upvote it btw)
Check your CSS, one quick way is through the F12 tools. @A. Wolff, I agree!

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.