Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating elements with jQuery

Tags:

html

jquery

css

I'm sure this is going to be obvious to the right person, but how can I repeat an element with jQuery? Esentially I want jQuery to repeat an inline element an infinite number of times; like you would use CSS to repeat a graphic for a background texture. I've been researching .clone() and .each() but but could really use some pointers.

Thanks!

  • J
like image 394
user1062823 Avatar asked Jan 18 '23 05:01

user1062823


2 Answers

http://jsfiddle.net/gRTTJ/

Key line:

$("#repeating").append($(".foobar").clone());

Although if you want it infinitely, it will be an infinite loop (obviously) and not be too useful.

A slightly more reasonable version that runs infinitely:

function repeat()
{

   $("#repeating").append($(".foobar:first").clone());
}

$(function () {

    setInterval(repeat, 1000);
});
like image 121
Jake Feasel Avatar answered Jan 24 '23 16:01

Jake Feasel


while(true) {
  $('#infinity-repeat').append('some text');
}

But that code will crash your browser. Better idea is to do it countable times.

like image 23
Hauleth Avatar answered Jan 24 '23 15:01

Hauleth