最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Executing a function onclick via ejs template - Stack Overflow

matteradmin4PV0评论

I am making a web application using node.js. I'm handling the server side via Express and using ejs templates to serve up the webpages. My issue is that I have a function in my index.js file and it is referenced in my ejs file in the button's onclick event. Currently I'm only logging a statement in the console to make sure it works. Now every time I restart the server and open up my home route, I see the message logged into the console but when I click the actual button, nothing happens. The fuction executes on the server start/restart but not when the button is clicked. I've tried writing the function entirely inside the ejs page using the correct ejs syntax and tags. Every time the same issue occurs; the function executes when the homepage is refreshed and logs the message in the console but nothing happens when the button is clicked.

I'm guessing since I'm calling the function in the res.render route, it gets executed once the page is rendered. How do I go about making changes so that the function executes once the button is clicked?

Here's the function for the button (inside index.js):

function clicker() {
  console.log("Button Working!");
};

Here's the index.js code that handles the route for this function:

app.get("/", (req, res) => {
  res.render("home", {
    x: clicker()
  });
});

Button Tag inside my home.ejs:

<button class = 'btn btn-primary' onclick = "<%=x%>">Click Me!</button>

Console view:

I am making a web application using node.js. I'm handling the server side via Express and using ejs templates to serve up the webpages. My issue is that I have a function in my index.js file and it is referenced in my ejs file in the button's onclick event. Currently I'm only logging a statement in the console to make sure it works. Now every time I restart the server and open up my home route, I see the message logged into the console but when I click the actual button, nothing happens. The fuction executes on the server start/restart but not when the button is clicked. I've tried writing the function entirely inside the ejs page using the correct ejs syntax and tags. Every time the same issue occurs; the function executes when the homepage is refreshed and logs the message in the console but nothing happens when the button is clicked.

I'm guessing since I'm calling the function in the res.render route, it gets executed once the page is rendered. How do I go about making changes so that the function executes once the button is clicked?

Here's the function for the button (inside index.js):

function clicker() {
  console.log("Button Working!");
};

Here's the index.js code that handles the route for this function:

app.get("/", (req, res) => {
  res.render("home", {
    x: clicker()
  });
});

Button Tag inside my home.ejs:

<button class = 'btn btn-primary' onclick = "<%=x%>">Click Me!</button>

Console view:

Share Improve this question asked Oct 17, 2020 at 20:20 HamzaHamza 591 gold badge2 silver badges8 bronze badges 1
  • You are confusing client side and server side code – charlietfl Commented Oct 17, 2020 at 20:45
Add a ment  | 

1 Answer 1

Reset to default 3

You are calling the function itself instead of passing it:

app.get("/", (req, res) => {
  res.render("home", {
    x: clicker() // -> THIS GET EXECUTED
  });
});

Istead you should write

app.get("/", (req, res) => {
  res.render("home", {
    x: clicker // -> THIS PASS AS OBJECT/FUNCTION
  });
});

Now it should get executed when you click on the button.

BUT: Im not sure if you define "clicker" on the server side, its gets passed to the client. The "clicker" function must be available on the client side, not server side. Move the defition on the client side and reference only the function name:

{
  x: "clicker"
}
Post a comment

comment list (0)

  1. No comments so far