I am trying to attach a piece of code to a button OnClick attribute.
// Creating a Button
var btn = document.createElement("BUTTON");
// Append a text to the button
var t = document.createTextNode("random text");
btn.appendChild(t);
// attach the function to the eventLisenter
btn.addEventListener('click', handleClick);
// function to be executed
var handleClick = function (event) {
alert("DBZ");
}
The button appears where it belongs, and so does the text in it, however when I press the button the code inside the function won't execute, nothing happens. I have tried running the code in Firefox & Google Chrome.
I have also tried to change the "click" attribute to "onclick" attribute, add/remove bracket of function when attaching and also use the btn.attachNow method. nothing happend really.
Would appreciate any help, Thanks in advance.