admin管理员组文章数量:1025269
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
1 Answer
Reset to default 3You 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"
}
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
1 Answer
Reset to default 3You 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"
}
本文标签: javascriptExecuting a function onclick via ejs templateStack Overflow
版权声明:本文标题:javascript - Executing a function onclick via ejs template - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612177a2159069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论