Adding dynamic buttons instead of replacement
I use this to create additional buttons.
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
var parentDIV = document.getElementById('extraDIV');
parentDIV.innerHTML = newVr;
But this replaces the existing buttons in parentDIV
. How do I add buttons instead of replacing buttons?
+3
source to share
2 answers
You have to use the operator +
for string concatenation. operator +=
means the string is concatenated with the current value:
var newVr = "";
for(i=0; i<5; i++){
newVr += '<button type="button" class="abc">New</button>';
}
var parentDIV = document.getElementById('extraDIV');
parentDIV.innerHTML += newVr;
<div id="extraDIV">
<button>Old button</button>
<button>Old button</button>
</div>
This works for me
+3
source to share