Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.3K
Visualizações
How to call a javascript function in ejs

I've tried adding an onclick function to my ejs button but it's not calling the function in my index.js. here is my ejs button in the views folder

<button onclick="test();">test</button>

here is my index.js

const express = require('express');
const app = express();



const ejs = require('ejs'); //to connect to ejs files
const path = require('path'); //to use redirect and render
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs'); //specify engine



app.get('/', (req, res) => {        
  res.render("index")
})

function test() //here is the function I want called.
  {
    console.log("works")
  }


app.listen(3000)

How do I call a function from ejs? If it is not possible, then how do you change varibles? ex. pg+=1. So the pg in the link increases by 1.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

In ejs or any technology, we cannot call a function front-end side which is declared in back-end(Server side). For that you have to send a http request to the server.

Or if you want to call test() for onclick then you have to make a one script file and declare that function in that file. and load that script file using <script src="YOUR FILE PATH"></script>

After that you can call a test function on click.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda