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.
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.