I am trying to make a function where I can just put showAlert()
after an element name and it will show an alert when clicked. I have defined my showAlert function in my JavaScript, but I always get an error.
I've tried:
function showAlert() {
return onclick = "alert('Hello World')"
}
document.getElementById('test').showAlert();
The code above should return: "document.getElementById('test').onclick = "alert('Hello World')
",
However, I get an error saying: document.getElementById(...).showAlert is not a function
Thanks for any help.