We all know that whenever you appendChild the Child will keep on appending. My question is how can you control the append child to only append once, twice, three times or any number of specified times. I'll give an example below, but I'm sure you'll already know what I'm talking about. A Jquery or Javascript answer is appreciated. Thanks.
Jquery, but could just as easily be pure javascript
$( document ).ready(function() {
$('#myBtn').click(function(){
$('p:eq(1)').css('background-color', 'yellow');
var newDiv = document.createElement('div');
newDiv.id = "newDiv";
$('#myDiv').append(newDiv);
newDiv.style.height = "25px";
newDiv.style.width = "25px";
newDiv.style.border = "2px solid blue";
});
});
HTML
<div id="container">
<div id="myDiv"></div>
<p>First Paragraph</p>
<p>Second Paragraph</p>
<p>Third Paragraph</p>
<input type="button" id="myBtn" value="click me" />
</div>
appendChildis in a loop, it only runs one. It's not a recursive function or anything..length, and use anif...thenstatement to conditionally ignore further appends.