"Implement a feature where HTML elements trigger a function instead of using the

<%- include("partials/header") %>
<p> this is main page</p> 
  
<% let cpp= 6 %>
<%  for (let i=0;i<cpp;i++){ %>
<div class="card">
  <li><%= cards[i].name %></li>
  <li><%= cards[i].price %></li>
  <li><%= cards[i].title %></li>
  <li><%= cards[i].inStore %></li>
  <li><%= cards[i].code %></li>
</div>
<% } %>
<div>button id="next">load more</button></div>

<%- include("partials/footer") %>

How can I update the value of cpp from 6 to 12 when the button with id="next" is clicked?

Answer №1

It's important to note that in order for the array cards to be populated, it should originate from the res.render statement within your JavaScript file. This is because there is no existing declaration of let cards.

res.render("ejsfile", {cards: [...]});

To address this, include the cpp integer within the same call and remove the unnecessary let cpp = 6 statement:

res.render("ejsfile", {
  cards: [...],
  cpp: Number(req.query.cpp) || 6 // setting a default value
});

This setup allows the initial 6 entries to be accessed at http://server/mainpage?cpp=6, and also at http://server/mainpage where '6' serves as the default value. Subsequently, the "next" button can direct users to http://server/mainpage?cpp=12.

For further information on how to utilize req.query, refer to the documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Struggling to generate an HTML table using JSON data

Struggling with generating an HTML table from JSON data, I'm inexperienced and having trouble with the logic. The JSON data I have needs to populate a complex dynamic HTML table. The design of the table is intricate, making it challenging for me to i ...

What steps can be taken to avoid special characters in ion-input fields?

When inputting special characters into the field used for storing the alphanumeric serial number, they are accepted. I need to prevent special characters from being entered in the input field. <ion-input [(ngModel)]="serial_number" (ngModelCha ...

Is there a way to stop a specific route in Express from continuing further execution without completing its function?

In my post route, I need to ensure that the user has provided all the necessary data in the body. To achieve this, I have added an if block to check for errors. router.post("/", (req, res) => { if(req.body.age < 24) { res.send("You are too you ...

Advice for resolving problems with npm path and installation

I have recently switched my nodejs installation to the D:\ drive instead of the C drive and updated the environment variables accordingly for the node & npm folders. After that, I modified the npm installation path by setting "prefix=D:\node&bso ...

The way in which the DOM responds to adding or deleting elements from its structure

My typical method for displaying a popup involves adding an empty div tag and a button to the webpage: <div id="popupDiv"></div> <input type="button" id="popupButton" /> I then use jQuery to handle a button click event, make an ajax cal ...

How should I organize mySQL code within a nodejs application?

Lately, I've been working on a project and utilizing node.js as my backend. Instead of using MongoDB, I've opted for MySQL as my DBMS due to specific requirements in my project. However, I'm facing challenges in structuring my MySQL queries ...

What is the process for including a custom Jasmine matcher definition in Typescript?

I've been searching around for information on creating custom Jasmine matchers using TypeScript and it seems like a common issue. However, none of the solutions I've come across have worked for me. My setup includes: { "typescript": "2.3.2", ...

Using JavaScript and HTML, showcase the capital city of a country along with its corresponding continent

As a newcomer to this platform, I am excited to share a code snippet that I have created using HTML and JavaScript. The code generates a textbox in an HTML page where users can type the name of a country. Upon inputting the country's name, the code dy ...

Exploring the integration of data from two Firestore collections using JavaScript

I manage two different types of collections, one being called CURRENCY-PAIR and the other Alerts. The collection CURRENCY-PAIR includes the following information: Currency-Pair Name Currency-AskPrice Currency-BidPrice On the other hand, the Alerts colle ...

Tips for creating an editable div that can also incorporate a textarea and the option to upload or delete photos

On my website, users can upload images, names, and descriptions which are saved in a MySQL database. The information is then fetched to display on the website. The code below allows users to enter this information and see it displayed when they click the s ...

Using Javascript, verify if a given URL is legitimate and commences with "http://" or "https://"

I need to validate the authenticity of my URLs, ensuring they begin with either http:// or https://. Here is the regular expression (RegExp) I have been using: private testIfValidURL(str) { const pattern = new RegExp('^(https?:\\/&bsol ...

Caution: A duplicate key was found in ReactJS when attempting to flatten children

Currently, I am utilizing Tabs from Material UI to showcase a List component that is filtered by the tab. Take a look at the code snippet below from my Container Component: <Tabs className="DrawerTabs" ...

Using jQuery Ajax and PHP for Secure User Authentication

Hello everyone, I could really use some assistance right now. The issue I'm facing is that the error div is not showing the content I expect in the success function of my AJAX request. I've tried alerting the response, which returns true if the ...

Is it possible to retrieve the controller path for an AJAX request from within a partial view?

Looking for a solution to fully decouple and reuse a partial view that allows users to select dates and filter results based on those dates. This widget can be used on multiple pages, so I wanted to add event listeners that would submit the form within the ...

Exploring Nextjs with server-side rendering and fetching data from

When utilizing the getServerSideProps function in Next.js to make a fetch request to my API, I encountered an issue where the origin header was coming back as undefined. This seems to be specific to requests made from the server in Next.js, as I am able ...

Extracting a variable established on the client side and passing it to a server-side node backend

I am currently working on creating a comments section for a web application. The front end of the app displays information about the threading level of each comment. When users submit comments, I need to pass this threading information to the back end in o ...

The "keydown" event in React will not alter the state

I am currently developing an application that requires me to track the keys pressed by the user. I am utilizing keydown and keyup events for this purpose. However, I am facing a challenge where I do not want the same key to be registered multiple times whe ...

What is the method for making a POST request using express js?

I'm struggling to extract the post variables from the request object in my app.post method. It seems like there might be a problem with how I am handling the request. Do I need to take any additional steps to correctly process the request? I attempted ...

Getting a variable from outside of the observable in Angular - a step-by-step guide

I have an Observable containing an array that I need to extract so I can combine it with another array. this.builderService.getCommercialData() .subscribe( data=>{ this.commercialDetails = data; this.commercialDetailsArr ...

Is there a way to prevent the "undefined" message from displaying in the console when this code is run?

Help needed! Can someone assist me in resolving the issue where the word 'undefined' is being displayed in the console? I'm a beginner in programming and struggling with this. Here's what I'm currently seeing: You are not getti ...