-3

I know how to create a button with userscript, but I don't know how to attach a function to the button with userscript. How do I attach a function to the button with userscript? I would also like to add an "id" to the button with userscript. How do I do that? Thanks.

var button = document.createElement("BUTTON");        // Create a <button> element
var text = document.createTextNode("CLICK ME");       // Create a text node
button.appendChild(text);                                // Append the text to <button>
document.body.appendChild(button);

function clickMe() {
alert("Hi");
} //end of function clickMe()
frosty
  • 2,559
  • 8
  • 37
  • 73
  • 2
    [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) – p.s.w.g Feb 19 '19 at 20:58

1 Answers1

-2

To attach a function to the button with userscript

button.addEventListener("click", clickMe, false);
Rohit Shetty
  • 494
  • 3
  • 8