0

I want to make each button call a function called sDM() and input a different code but whenever I do this, I get:

ReferenceError: sDM is not defined at HTMLButtonElement.onclick

<button onclick="sDM(38)" style="width: 100%">&#8593;</button>
<br>
<button onclick="sDM(37)"  style="width: 47%">&#8592;</button>&nbsp;
<button onclick="sDM(39)" style="width: 47%">&#8594;</button>
<br>
<button onclick="sDM(40)" style="width: 100%">&#8595;</button>

My function:

  function sDM (dN) {
           snake.setDirection(directions[dN])
         }
1
  • Please add your HTML code as well. Commented Jan 26, 2018 at 2:24

1 Answer 1

1

(could be)Looks like that your javascript is loaded before your html. Please insert your javascript inside one of those listeners

document.addEventListener( 'DOMContentLoaded', function( event ) {
    // Do something
});

or

window.addEventListener( 'load', function( event ) {
    // Do something
});

Here's the explanation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.