jQuery Plugin, $.repeat()
While working on the Knit Project, I discovered that I needed a way to repeat a string and then insert it into a jQuery/DOM object. So I created this little bit of code to do just that.
//Repeater Plugin
jQuery.fn.repeat = function(times, string) {
//For each item matched
this.each(function(){
var buff = string;
for(var i=1; i < times; i++){
buff += string;
}
jQuery(this).append(buff);
});
return this;
}
The plugin takes two parameters, the number of times to repeat, and the string to repeat. this means that if I want to add a bunch of columns to my table rows, all I have to do is:
//Add the columns
$("table > tr").repeat( width, "<td></td>" );
Now all my table rows will have the correct number of columns.
I used .append instead of replacing the old data just in case I would ever need to keep what is already there. This could easily be changed or even turned into a switch.
If this was what you were looking for, or even if it wasn’t, please leave a comment. I’ve gotten a lot of traffic from this little plugin and I would like to know if people find it useful.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

I am really thankful to this topic because it really gives useful information ;.;
brooklyn escort service 1468 Nostrand Ave, NY 11226 917 675-5039
Hello,
A useful function… I think, it would be a bit faster if we write it this way, in case if we have selected several objects …
//Repeater Plugin
jQuery.fn.repeat = function(times, string) {
var buff = string;
for(var i=1; i < times; i++){
buff += string;
}
$(this).append(buff);
return this;
}
Thanks for the idea!
hi,
i need to wite jquery for continuous vertical rotating html text in a table.
Can you help?
neelam
Maybe faster:
var arrCell = [];
for(var cellCount = 0; cellCount < 100 ; cellCount++)
arrCell[cellCount] = '’;
$(‘table tr’).append(arrCell.join(”));
That it is actually a nice and useful part of information. So i am happy you shared this helpful info along with us. You should keep informed like this. Many thanks for sharing.
Simply desire to say your article is as amazing. The clearness for your post is just great and i could assume you’re a professional in this subject. Well along with your permission allow me to clutch your RSS feed to stay up to date with coming near near post. Thanks 1,000,000 and please carry on the enjoyable work.