0

I have am doing an app in Express with EJS and so far it was easy in respect with EJS. My issue is that I am trying to call a function with the onclick event handler.

I want to call the function

function click() {
    console.log("You have clicked")
}

in controller I have the endpoint

exports.getPortfolioOnDue = async (req, res, next) => {
    let data, click;
    try {
        data = await listBlobs(matchExp, containerClient, res)    
    } catch (error) {
        res.send(error)
    }
    res.render('portfoliodue', { isAuthenticated: req.session.isAuthenticated, configured: isConfigured(req), data: data, click: click });
}

in the EJS file I have:

<table class="table table-striped">
    <thead class="thead-light">
        <tr>
            <th scope="col">#</th>
            <th scope="col">List of reports</th>
            <th scope="col">Get item</th>
        </tr>
    </thead>
    <% for(let i=0; i < data.length; i++) { %>
        <tr>
            <th scope="row"><%= i + 1 %></th>
            <td><%= data[i] %></td>
            <td><button type="button" class="btn btn-outline-warning btn-sm" onclick="<%= click %>">Download</button></td>
        </tr>
    <% } %>
</table>

I mention that the loop, retrieve data an maps it correctly. the event handler onclick should evaluate the expression <%= click %>, but it doesn't.

Please help me, this should be easy and supposedly not losing time with it.

Thank you!

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
A B
  • 177
  • 1
  • 3
  • 13
  • Have you tried `click()` instead of `click` in the onclick attribute? – Jotha Sep 01 '21 at 12:16
  • 1
    Are you attempting to invoke *server-side* code with that *client-side* button? – David Sep 01 '21 at 12:16
  • @David yes, instead of that click function that should console.log out something, should be a function that retrieve a blob as buffer and send it to the browser as http response – A B Sep 01 '21 at 12:23
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – David Sep 01 '21 at 12:24
  • 2
    @AB: Refer to the proposed duplicate. Regardless of the server-side language used (JavaScript with NodeJS here, PHP in the duplicate) the structure is still the same. The server-side code runs to completion and then the page is sent to the browser and any client-side code is run in the browser. If you want to interact with the server again then that would involve another HTTP request. A form post, a link, or AJAX. – David Sep 01 '21 at 12:25
  • @David got it... so true. Many thanks!!! – A B Sep 01 '21 at 12:28

0 Answers0