Skip to content

Instantly share code, notes, and snippets.

@audinue
Created December 26, 2014 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save audinue/dcdc1379bb6aeb4116cc to your computer and use it in GitHub Desktop.
Save audinue/dcdc1379bb6aeb4116cc to your computer and use it in GitHub Desktop.
Repeat certain element in HTML.
/*
Usage:
<script src="repeat.js"></script>
Example:
<ul>
<li repeat="3">Hello World!</li>
</ul>
will produce:
<ul>
<li repeat="3">Hello World!</li>
<li repeat="3">Hello World!</li>
<li repeat="3">Hello World!</li>
</ul>
*/
document.addEventListener('DOMContentLoaded', function() {
Array.prototype.forEach.call(document.querySelectorAll('*[repeat]'), function(node) {
var n = parseInt(node.getAttribute('repeat')) - 1;
for(var i = 0; i < n; i++) {
if(node.parentNode.lastChild == node) {
node.parentNode.appendChild(node.cloneNode(true));
} else {
node.parentNode.insertBefore(node.cloneNode(true), node.nextSibling);
}
}
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment