Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!