When the button is clicked, the function will be executed as soon as

Upon page load, the function is executed regardless of whether the button has been clicked or not. It's important to note that I am utilizing EJS.

I experimented with changing the <% tag to <%= and <%-, and also attempted using an arrow function:

<% () => Delt(this.id);%>
.

<%function Delt() { Array.pop(); }%>
<button onclick="<%Delt(this.id);%>">Delete</button>

Answer №1

It seems like there may be some confusion between client-side and server-side code. Anything within the ejs tags is executed on the server before your DOM loads. Therefore, functions defined within ejs tags are executed because they are not in client-side js. It's recommended to define your functions in client-side JavaScript rather than within ejs, and the same goes for function calls.

<%function Delt() { Array.pop(); }%> // function definition in ejs
<button onclick="<%Delt(this.id);%>">Delete</button> // function call in ejs

Everything is executed on the server before your DOM loads. When you make a normal function call in ejs, it doesn't recognize your onclick event because that is part of your browser's functionality.

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

What is the process for updating the list to retrieve fresh data from the service?

I am currently in the process of calling a web service to retrieve data and display it in a list using directives. Upon loading my fiddle, I successfully load data from a JSON file. The data is displayed correctly in the list. If I click the delete butto ...

Setting up Stylelint in a Vue 3 app with VSCode to automatically lint on save

I am looking to perform linting on my scss files and scss scope within .vue components. Here is what my configuration looks like in stylelint.config: module.exports = { extends: [ 'stylelint-config-standard', 'stylelint-config-rece ...

Ordering elements of array [i] based on sorting criteria of [i][0] and [i][1]

Looking to organize an array consisting of sets or pairs based on the first number in each pair. Here is the code snippet: function randomMap(numOfHills){ for(var i=0; i<numOfHills; i++){ xRandomForHills = Math.floor((Math.random() * 2000) ...

Utilizing an array for substituting sections of a string: a guide

I have an array of values like ['123', '456', '789']. What is the best way to iterate over this array and update parts of a string that contain the text :id in sequence (e.g. users/:id/games/:id/server/:id)? Currently, I&apos ...

Issue with creating user using Passport-local (Node, Express, Postgres, Knex) causing a timeout

Currently, I am integrating Passport for authentication and authorization into my Node/Express API that serves data to a Vue frontend. I have a createUser function that successfully adds the user, but it seems to hang during the process. I believe I may ha ...

How to Retrieve a Scope Variable from a Controller within an Angular.js Directive

(function () { 'use strict'; angular.module('app') .controller('someController', ['$scope','localStorageService',someController]) .directive('someDirective', someDirective) function some ...

Tips for refreshing only a portion of a webpage using JavaScript/jQuery

I have two distinct navigational sections on my website. The left column has its own navigation menu, while the right column (main content area) contains a separate set of links: My goal is to click on a link in the left-hand sidebar (such as "Resume", "E ...

How to Ensure Screen Opens in Landscape Mode with Phaser3

https://i.stack.imgur.com/keCil.pngIn my Phaser3 game, I am trying to achieve the functionality where the game opens horizontally in a webview without requiring the user to rotate their phone. The vertical photo below shows the game in its current state. W ...

Having trouble with VueJS ref not preventing the default form action on submit?

Within my <script> tag, I currently have the following code: render(createElement) { return createElement("form", {ref: "formEl" , on: {submit: this.handleSubmit} }, [ <insert create form inputs here> ]); } handleSubmit(e) { ...

Error: The variable $ has not been declared in the context of the function iterateId

I understand that there are many questions on SO related to this error, but my issue is a bit unique. In my code, I am using a forEach loop to retrieve the IDs and text of elements as shown below: function iterateId(id){ $("input[id*='" + id + ...

Unable to initiate the server generated by the express.js generator

Currently, I am trying to set up an Express.js server using their generator. Following the documentation, I was able to successfully create the basic structure. However, when attempting to run the prescribed command (SET DEBUG=transcriptverificationserver: ...

What are the Steps to Deploy a React + Express Application on Heroku Platform?

My project folder structure looks like this: Client //Contains the react app Node_modules App.js //Where server code is located Package.json Package-lock.json I am looking to deploy it to Heroku. Within one of the files in the react app, I have: This is ...

Selecting checkboxes based on a delimited string containing specific values can be done using Javascript

I have a set of checkboxes similar to the ones below that I would like to select based on database values. If I convert the database values into a string format like DC0|100;DK0|100;DM0-SRM|200;DR0|300 (delimited by semicolons or another delimiter), how ca ...

Steps to stop mat-spinner upon receiving Job Success/Failure Notification from the backend

I have a task that runs asynchronously and takes a long time to complete. When the task starts, I display a mat-spinner with a timeout set at 60000 milliseconds. However, we now have a notification service that provides updates on the job status. I would l ...

"Encountering an error while attempting to map a JSON array in

I am just starting to work with knockoutjs and I'm attempting to map JSON data to KO. Here is my code: My goal is to parse the JSON data and display it in a table. However, when I try to use ko.compute, I encounter an error saying "self.firstName() i ...

Failed to parse module: Encountered an unexpected token while using babel-loader with BABEL_ENV set to development

I recently encountered an issue with my React project using webpack when I added the @superset-ui/embedded-sdk library via yarn. While the production build runs smoothly, the development build fails to compile. Since I am not very familiar with webpack, it ...

Shrink video size directly in the browser using JavaScript and HTML5 before storing or sharing it

Is there an optimal method for resizing and potentially trimming a video using Javascript, HTML5 or Webassembly in modern browsers before saving it to the Filesystem? What is the most effective way to edit a video within the browser? Here are some differe ...

The Instagram API seems to be experiencing some technical difficulties

My expertise lies primarily in HTML/CSS, with limited knowledge of using APIs and JavaScript. I have been assigned the task of utilizing an API to retrieve Instagram pictures posted by users with a specific hashtag. Here is the demo shared via JSFiddle: ...

Tips for integrating H4 - H6 using a text editor in DNN7

It is essential for my client to have access to at least H4. Although I can add H4 to the ApplyClass menu in the text editor, it only applies a <span class="h4"> Sample </span> tag within the paragraph itself. Unfortunately, this method does ...

I wonder where the file from the HTML form download has originated

Recently, I've been experimenting with the developer tools in Chrome to observe the behavior of websites at different moments. It has proven useful in automating certain tasks that I regularly perform. Currently, my focus is on automating the process ...