+ 3

button in webpage

hello this is my code for making a button when I push a button but it will disappear when I refresh the page. there any way to make it stay there always ?? <!DOCTYPE html> <html lang="en"> <body> <button id="myBtn" class="btn btn-info">Try it</button> <p id="demo"></p> <script> document.getElementById("myBtn").addEventListener("click", function(){ document.getElementById("demo").innerHTML =' <button id="myBtn" class="btn btn-info">Try it</button>'; }); </script> </body> </html>

18th Feb 2018, 6:53 AM
hossein hayati
hossein hayati - avatar
1 Answer
+ 9
DOM changes made by script are not persistent, the button you add to the paragraph will not stay in the document if the page is refreshed, one thing you may consider is to use a random id for the new button you place in the paragraph, we know id are supposed to be unique, it is not a good practice to create multiple elements having same id attribute assigned : )
18th Feb 2018, 7:03 AM
Ipang