I have a pug template where I want to call a function with some variables passed from the endpoint that renders the page:
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
button(type = 'button' onclick='play(#{x},#{y})')
script(type='text/javascript' src='../public/javascripts/play.js')
Here is play.js
:
const play = function(x,y){
console.log(x+y);
}
When I load the page, I get this error:
Uncaught SyntaxError: Unexpected token ':' play.js:1
How can I get pug to include the script correctly so that I can call the function with a button click?
EDIT: here's the rendered HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<button type="button" onclick="play(#{x},#{y})"></button><script type="text/javascript" src="../public/javascripts/play.js"></script>
</body>
</html>
EDIT 2:
I changed the interpolation to
onclick=`play(${x},${y})`
and this renders as:
onclick="play(2,3)"
which are the values that were passed in from the endpoint, but I'm still getting the unexpected token
error.