-1

I want to implement logic where after clicking into the button the counter will be incremented by 5 (5, 10, 15, 20...) and this, so to speak, state, would be used inside the for loop as a second argument (ie. how many loops should it proceed).

<body>
  <% var counter %>
  <% for (var i = 0; i < counter; i++) { %>
    ...some items to be rendered in number of 5, 10, 15 and so on...
  <% } %>
  <button onclick="<%() => counter =  counter + 5%>">Load more</button>
</body>

Above does not work. How can I achieve that kind of state in EJS? Thanks!

Murakami
  • 3,474
  • 7
  • 35
  • 89

1 Answers1

0

you just have to create a function in ejs which will repeat the step after each button click and on button click we are incrementing the counter an passing it to the function so the function will execute with incremented value.

   <body>
      <% var counter=5 %>
      <% function callLoop(cntr){%>
       <% for (var i = 0; i < cntr; i++) { %>
        ...some items to be rendered in number of 5, 10, 15 and so on...
       <% } %>
      <% } %>

      <button onclick="<%() => callLoop(counter + 5);%>">Load more</button>
   </body>