rohan32
rohan32

Reputation: 520

Jade button onClick not working

I currently have a file in jade and I'm attempting to make a form where if the user clicks on the submit button after filling out the question's answer, then it runs a JavaScript function. However, for some reason, it is not able to find the function in the file for some reason.

Below is my Jade code:

script(language='JavaScript').
  function checkAnswer() {
    var answer = document.getElementById('sessionID').value.toLowerCase();
      if(answer == '4') {
        console.log('Correct!');
      }
      else {
        console.log('Sorry, try again!');
      }
  }

extends ../layout

block content
  .page-header
    h3 Challenge #1
  p 2 + 2 = ?
  input(type='text', name='sessionID', value=valueID)
  input(type='button', value='Submit', onclick='checkAnswer()')

This returns the error code:

Uncaught ReferenceError: checkAnswer is not defined

What am I doing wrong? It must be something really stupid :(

Upvotes: 1

Views: 6083

Answers (2)

rohan32
rohan32

Reputation: 520

Fixed it with the help of the person above - even though others say it wasnt specific enough to help, it was good enough for me :)

Here is how you would fix that. Since its outside of the "block" simply move the script into the block and it will now be compiled into HTML from Jade. Personally, I also moved the JS to a different file and instead of describing the function right in the jade file I moved it to another JS and included it in the jade file with the line

script(type='text/javascript' src='answers.js')

Thanks again for the help!

Upvotes: 2

spelufo
spelufo

Reputation: 634

You need to put the script inside some block for it to be inserted in the resulting html file.

Upvotes: 0

Related Questions