I'm trying to learn a bit of ejs and node, I know that I can easily create a pop-up modal with only JS and HTML like this calling a function when I click certain button
Button to call the function:
<button id="myBtn">Open Modal</button>
Function:
btn.onclick = function() {
modal.style.display = "block";
}
But how can I do this with Ejs and NodeJs?
I'm trying to learn a bit of ejs and node, I know that I can easily create a pop-up modal with only JS and HTML like this calling a function when I click certain button
Button to call the function:
<button id="myBtn">Open Modal</button>
Function:
btn.onclick = function() {
modal.style.display = "block";
}
But how can I do this with Ejs and NodeJs?
Share Improve this question asked Jan 2, 2020 at 14:05 KAK4SHI HATAK3KAK4SHI HATAK3 1171 silver badge9 bronze badges1 Answer
Reset to default 6It doesn't make sense to do this with EJS and Node. In this context those are tools for running server-side code. It makes more sense to use the existing JS you have as client-side JavaScript (in the HTML document you are generating from the EJS).
That said, if you really wanted to do it server-side, then you'd want something like this in the HTML:
<form method="post">
<button name="modal" value="open">Open Modal</button>
</form>
And then (assuming you are using Express JS) something like:
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.post("/your/page", function (req, res) {
const show_modal = !!req.body.modal; // Cast to boolean
res.render("page", { show_modal });
}
And then in the EJS:
<% if (show_modal) { %>
<div id="modal" style="display: block">
etc
</div>
<% } %>