jeudi 29 avril 2021

Onclick "run a function" using external javascript

I created a simple project in which I click on a button then another button should be created(button 2) and the previous button should be disappeared and this works fine. But when I click on button 2 then a particular function should be executed. I am stuck here.

It says cannot set property to null.
function myFunction1() {
  var x = document.createElement("BUTTON");
  var t = document.createTextNode("button 2");
x.setAttribute('id','bt2');
  x.appendChild(t);
  document.body.appendChild(x);

var a= document.getElementById('bt1');  
a.style.display='none';

}
var item = document.getElementById('bt2');
item.onclick = myFunction;
function myFunction() {
  document.getElementsByTagName("BODY")[0].style.backgroundColor = "lime";
    }
<button id= 'bt1' onclick="myFunction1();">
 button 1
</button>



Aucun commentaire:

Enregistrer un commentaire