#10 - Turn a script into a button

Data: 2018-05-26 12:00 - JS

Simple example how to replace a script tag with a button.

// Get DOM object of current script
var script = document.currentScript;

// Create new button element
var newButton = document.createElement('button');
newButton.textContent = 'Hello world!';
newButton.addEventListener('click', function() { alert('hello'); });

// replace the script with the button
script.parentNode.replaceChild(newButton, script);

Previous snippet | Next snippet