Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

391
Vistas
trying to include script in pug and call a function from the script, getting "unexpected token" error

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.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda