This loop (see jsfiddle) tries to append a <span> tag to a container five times. But it only does it once. Why?
var myArr = [0,1,2,3,4];
var $span= $('<span></span>');
for (var i = 0; i < (myArr.length); i++) {
$('.contain').append($span);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem is that you’re appending the same element multiple times.
Use
cloneto clone the element and thenappend.$('.contain').append($span.clone());demo
Update:
This way you can customize your element and then clone it with all properties.
demo2
Update2: according to this comment.
$('.contain').append('<span class="yourClass"/>');