javascript - Why wont this on.Click event not work -


///// form  //// var form = document.getelementbyid("form"); form.setattribute("style", "position: relative; margin-bottom: 5%;");  var newbutton = document.createelement('button'); var newul = document.createelement('ul'); var newli = document.createelement('li'); var newinput = document.createelement('input'); form.appendchild(newbutton); newbutton.setattribute("style", "position: absolute; width: 15%; height: 40px; left: 42%; color: #b00000; background-color: white; font-size: 18px; border: 0px; "); newbutton.innerhtml = "direct message"; newbutton.onclick = function() {   alert("works"); }; 

is there wrong selection? want on.click work on button newbutton created element.

javascript property names case-sensitive, should newbutton.onclick.

or move modern age , use addeventlistener.

newbutton.addeventlistener("click", function() {     alert("works"); }); 

Comments