-2

I have a form and inside it there is a button.

<form>
  <button type="button" onclick="hello(); return false;">Hi</button>
</form>

I'm trying to call hello which is in my javascript file but I'm getting the error:

(index):43 Uncaught ReferenceError: hello is not defined at HTMLButtonElement.onclick ((index):43)

I'm using JSFiddle. My JavaScript is located in the JavaScript panel.

Why am I getting this error and how do I fix it (in JavaScript)?

template boy
  • 10,230
  • 8
  • 61
  • 97

1 Answers1

-1

Place your javascript function inside body tag, it resolves the issue

<!DOCTYPE html>
<html>
<body>

<form>
  <button type="button" onclick="hello(); return false;">
  Hi
  </button>
</form>

<script>
function hello() {
    alert("Hello");
}
</script>

</body>
</html>
chans
  • 5,104
  • 16
  • 43