Search code examples
javascripttampermonkeyjquery-click-event

Trying to make a Alert button in tampermonkey


I have been trying to get a alert button working in tampermonkey but i dont seem to get it working. Here is my script;

var button = document.createElement("Button");
button.innerHTML = "Title";
button.style = "top:15px;left:15px;position:absolute;z-index:99999; width:50px; height 50px; background_color; ff0000";
button.onclick = "alert('Test Alert')"
document.body.appendChild(button);

Solution

  • You can only assign a function to onclick. If you assign a string, no function will be executed when a click occurs:

    var button = document.createElement("Button");
    button.innerHTML = "Title";
    button.style = "top:15px;left:15px;position:absolute;z-index:99999; width:50px; height 50px; background_color; ff0000";
    button.onclick = () => alert('Test Alert');
    document.body.appendChild(button);