Becky
Becky

Reputation: 5585

Add dynamic buttons instead of replacing

I'm using 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 the buttons?

Upvotes: 3

Views: 38

Answers (2)

Chris Rosete
Chris Rosete

Reputation: 1258

Also you can use jQuery append:

$("#extraDIV").append(newVr);

Note: For jQuery you need to add the link on the header something like:

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

Upvotes: 0

You must use the + operator to concatenate strings. The operator += means that the string is concatenated to 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>

It works for me

Upvotes: 3

Related Questions