It's a user alert script. It worked in every browser I tested. But the function itself looks very messy/repetitive. Is there any way to make this cleaner?
function message(text) {
var alert = document.createElement('div');
var alertText = document.createTextNode(text);
var alertCloseButton = document.createElement('i');
var buttonText = document.createTextNode('close');
var shadow = document.createElement('div');
alert.className = 'alert-text';
alertCloseButton.className = 'material-icons';
alertCloseButton.id = 'close-alert';
alertCloseButton.setAttribute('onclick', 'function() { var a = this.parentNode; var s = a.parentNode; s.parentNode.removeChild(s)}');
alertCloseButton.onclick = function() { var a = this.parentNode; var s = a.parentNode; s.parentNode.removeChild(s) };
alertCloseButton.appendChild(buttonText);
alert.appendChild(alertCloseButton);
alert.appendChild(alertText);
shadow.className = 'shadow';
shadow.appendChild(alert);
document.body.appendChild(shadow);
}
var button = document.querySelector('button');
button.addEventListener('click', function(){
message('Button clicked');
});
Here's the code working: https://jsfiddle.net/3aouyfz7/