node.js - ejs include on click -
i'm trying create dynamically included in ejs page (using <%- include('partials/content') %>
) on node.js project. there way can create variable to-be-included page , change on button click?
let's assume partials/content
file includes content like:
<h1>lorem ipsum dolor sit amet</h1>
a file named partials/content2
:
<h1>consectetur adipiscing elit<h1>
your main template file wrap partials content <div>
id result
, include script file select <div>
using var $result = $('#result');
have in variable. can register click handler on button. on click request wished template file , replace content.
main template:
<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> </head> <body> <div id="result"> <%- include partials/content.ejs %> </div> <button id="change">change</button> <script> var $result = $('#result'); $('#change').click(function() { $result.load('/content2'); }); </script> </body> </html>
then need controller on backend like:
app.get('/content2', function (req, res) { res.render('partials/content2'); });
Comments
Post a Comment