0

HTML:-

<button type="button" onclick="alert()">Want a greeting?</button>

Javascript:-

function alert(){ alert('working...') }

refference image

victor
  • 1
  • Notice also, while `alert` is a predefined name, the preview of the fragment editor _will not execute_ your JS event handlers. That's completely expected, as well as links will be deactivated, so that you don't navigate away from the fragment editor through clicking on your fragment. – Olaf Kock Oct 19 '21 at 06:34

3 Answers3

1

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>
Andy
  • 61,948
  • 13
  • 68
  • 95
0

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>
Nathan Champion
  • 1,291
  • 1
  • 14
  • 23
0
<button type="button" onclick="click()">Want a greeting?</button>

<script> let click = () => {alert('working...');}</script>
  • In Liferay the script is not working – victor Oct 18 '21 at 09:59
  • 1
    @victor Then I would create a new question with the code with the fixed function name. Make sure you specify what exactly you mean by "not working". – General Grievance Oct 18 '21 at 12:41
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 18 '21 at 13:47