0

I am new to JS so please bear with me :)

below is the code but on clicking it does not trigger the function

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button type="submit" onclick="evaluate()">Submit</button>
</body>
<script type="text/javascript">
    function evaluate(){
        document.write("Working");
    };
</script>
</html>
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. This is one of the reasons why. – Sebastian Simon Oct 20 '21 at 19:17

2 Answers2

1

evaluate() is a reserved function in JavaScript. Name your function something else.

The fact that the console error mentioned the need for two arguments was a clue that the function was defined somewhere already, and that your definition wasn't being considered.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <button type="submit" onclick="notEvaluate()">Submit</button>
</body>
<script type="text/javascript">
  function notEvaluate() {
    document.write("Working");
  };
</script>

</html>

Regarding document.write, see Why is document.write considered a "bad practice"?.

isherwood
  • 58,414
  • 16
  • 114
  • 157
0

Try using a function name different than "evaluate" - evaluate is a reserved function name

Jon Sisk
  • 3
  • 3