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>
      

Run codeHide result


This works for me

+3


source


Also you can use jQuery append:

$ ("# extraDIV") add (newVr).



Note. For jQuery, you need to add a header link something like this:

<script src="https://code.jquery.com/jquery-2.1.1.js"></script>

      

0


source







All Articles