0

I know there are a lot of posts regarding this, but I've gone through them and I can't figure out why this doesn't work.

I'm trying to call a javascript function from an HTML button. However, it's not working. It keeps saying it can't find the function. I get a

Uncaught TypeError: guess is not a function

<body>
<script type="text/javascript">
   function guess() {
      alert("called");
  }
</script>
<div>
   <form name="Guess">
      <div data-role="fieldcontain">
      <label>Guess</label><br/>
      <input type="text" id="guess" maxlength="1"/><br/>
        <button data-action="bea" onclick="guess()" class="btn">
         Guess

       </button>
    </div>
  </form>
 </div>
 </body>
Gakho
  • 603
  • 1
  • 9
  • 18
  • `guess` is not a function, it's an `` ;) – Niet the Dark Absol Feb 17 '16 at 20:19
  • `guess` in the scope of the `onclick` attribute is the `` element with the `id` `guess`. See the duplicate question for an explanation of what `onclick` does with `with`. Avoid `onclick` attributes. – Quentin Feb 17 '16 at 20:19
  • Move the button out of the form, _or_ change the ID of your `input` field from `guess` to literally anything else. – jperezov Feb 17 '16 at 20:31
  • 1
    In addition, invoking event handlers using inline html attributes is not recommended. Wire up event handlers using object.addEventListener(eventName, callback); – Scott Marcus Feb 17 '16 at 20:31

0 Answers0