Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button clicks in Jade

I'm having difficulty getting a button to execute a javascript function when it is clicked, below is my jade file

extends layout
block content
  - var something = function() {
  -   console.log('something')
  - }

  button(onclick='#{something()}') Click

Where am I going wrong with this?

like image 502
Philip O'Brien Avatar asked Mar 28 '14 14:03

Philip O'Brien


2 Answers

With this line:
button(onclick='#{something()}') Click

you tell Jade that it should paste the content of the function into the value of the onclick attribute.

Just reference the function name:

button(onclick='something()') Click

But this won't work, because the function is only available in the Jade compile step. After this step the generated HTML has no access to the variables which was defined in Jade.

You need to include a JavaScript file or use the script tag:

script.
  var something = function() {
    console.log('something')
  }

button(onclick='something()') Click
like image 76
timaschew Avatar answered Oct 02 '22 09:10

timaschew


Just add a space:

button(onclick='#{something()}') Click
like image 30
Evgeny Samsonov Avatar answered Oct 02 '22 07:10

Evgeny Samsonov