Lucas Karpinski
Lucas Karpinski

Reputation: 41

Why is my onclick inPug not calling my js function?

I am attempting to call a function in my js file from the html side. Currently, I am using PUG my for templating engine and cannot get the onclick to work.

Views/client.js

function doSomething() {
  alert("Just work");
}

Views/customize.pug

button(onclick='doSomething()') fail
    <script src='client.js'></script>

If I copy over my function to customize.pug. And add it in using

script.
function doSomething() {
  alert("Just work");
}

It works just fine. However, I can never call the function in client.js

Upvotes: 0

Views: 530

Answers (1)

Stephen S
Stephen S

Reputation: 3994

If you are using express, your client-side javascript should be in a public directory exposed using express.static. Your client.js in the Views directory is not directly accessible without a explicit route.

You can expose the entire public directory in app.js/index.js

app.use(express.static(path.join(__dirname, 'public')));

Place the client.js as public/javascripts/client.js

And in your Views/customize.pug you can include it

button(onclick='doSomething()') fail
script(src='/javascripts/client.js')

Upvotes: 2

Related Questions