Frans B Frans B - 1 year ago 90
Javascript Question

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');
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() {

Is there something wrong with my selection? just want the to work on my button which is newButton created element.

Answer Source

Javascript property names are case-sensitive, it should be newButton.onclick.

Or you could move into the modern age and use addEventListener.

newButton.addEventListener("click", function() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download