Shon Aufdaded Shon Aufdaded - 1 year ago 86
Javascript Question

Appending a button using Javascript without using the createElement method

<script>
var i=0;
var j="";
function app()
{
j+=document.getElementById("demo").innerHTML="<button type='button'>Btn+i</button>";

}
</script>
<button type="button" onclick="app()">Add</button>
<div id="demo">
</div>


I am trying to append a button in the div tag every time user clicks add button. Also I want the added button to be displayed like Btn 1, Btn2, Btn3..

Answer Source

A simple concatenation of #demo content and incrementation of a var i:

var i = 0;

function app() {
  document.getElementById("demo").innerHTML += "<button type='button'>Btn " +
    ++i + "</button>";
}
<button type="button" onclick="app()">Add</button>
<div id="demo"></div>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download