HTML:-
<button type="button" onclick="alert()">Want a greeting?</button>
Javascript:-
function alert(){ alert('working...') }
HTML:-
<button type="button" onclick="alert()">Want a greeting?</button>
Javascript:-
function alert(){ alert('working...') }
Call your function anything other than alert
. It's a reserved keyword, and the error your code gives you is:
InternalError: too much recursion
function handleClick() {
alert('working...');
}
<button type="button" onclick="handleClick()">Want a greeting?</button>
alert() is a built-in method of JavaScript.
https://developer.mozilla.org/en-US/docs/Web/API/Window/alert
If you change the name of your function that will fix the issue:
function test(){ alert('working...') }
<button type="button" onclick="test()">Want a greeting?</button>
You can override the alert method but you can't recursively call the same function. The issue you're running into is an infinite recursion, you're calling alert > alert > alert > alert > ...
Something like this would work, though.
window.alert = function alert(message){ console.log(message) }
<button type="button" onclick="alert('test')">Want a greeting?</button>
<button type="button" onclick="click()">Want a greeting?</button>
<script> let click = () => {alert('working...');}</script>