Skip to content Skip to sidebar Skip to footer

Javascript: Add Content On Button Click

i'm trying to use javascript to add new content on a button click. I have got the javascript to work if the button is clicked once but i would like to have it so every time i pres

Solution 1:

Change:

document.getElementById("demo").innerHTML = "Hello World";

to:

document.getElementById("demo").innerHTML += "Hello World";

Using = will set the content on each click, overwriting what was there before. Using += will concatenate the text to what was there before.

 document.getElementById("myBtn").addEventListener("click", function() {
   document.getElementById("demo").innerHTML += "Hello World ";
 });
<button id="myBtn">Try it</button>

<p><strong>Note:</strong> The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.</p>

<p id="demo"></p>

Post a Comment for "Javascript: Add Content On Button Click"