Shon Aufdaded Shon Aufdaded - 1 year ago 73
Javascript Question

Appending a button using Javascript without using the createElement method

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

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

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