Created
December 26, 2014 16:49
-
-
Save audinue/dcdc1379bb6aeb4116cc to your computer and use it in GitHub Desktop.
Repeat certain element in HTML.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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