"By implementing an event listener, we ensure that the same action cannot be

function addEventListenerToElement(element, event, handlerFunction) {
  if(element.addEventListener)  {
    element.addEventListener(event, function(){
      handlerFunction(this.getAttribute("src"));
    }, false);
  }
}

//initialize the function 
addEventListenerToElement(".Dicon", "click", handleClick);

function handleClick(){
console.log("Clicked");
}

I am using this function to enable my image click functionality, but I don't want someone to repeatedly spam clicks on it. How can I prevent users from continuously clicking it?

Answer №1

If you want to prevent users from clicking on a certain element, you can visually indicate that it is disabled.

function addClickEventListener(element, clickHandler) {
  if (typeof(element.addEventListener) === 'function') {
    element.addEventListener('click', clickHandler, false);
  }
}

function disableElementOnClick(event) {
    try {           
        // Disable the clicked element
        event.target.disabled = true;
        console.log("Element disabled");
    } catch (error) {}
}

// Start the process 
var clickableElements = document.getElementsByClassName("Dicon");
for (var i=0; i<clickableElements.length; i++) {
    addClickEventListener(clickableElements[i], disableElementOnClick);            
}

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

Choose key and value pairs in AngularJS

Currently, I am retrieving JSON values from an HTTP service for each ID and attempting to create a dropdown with a list of names and IDs as keys to store. Below is the HTML code snippet: <select> <option ng-repeat="(key, value) in data" value="{{ ...

Remove an item from the DOM instantly with React

Having trouble synchronously removing a child from the container? Here is a simplified code snippet demonstrating the current solution using the useState hook. type ChildProps = { index: number; id: string; remove: (index: number) => void; }; fun ...

Receiving an 'undefined' result in an asynchronous response even with 'await' and 'then' statements implemented

I'm struggling with sending a GET request, parsing the response, and passing it to another function. It seems like I'm having difficulty dealing with the asynchronous nature of the response. I prefer to stick to using Node.js' built-in modu ...

Adjust the body class dynamically based on the URL in AngularJS

Here is the link to my website To Log In - http://localhost/ang/#/login To Access Dashboard - http://localhost/ang/#/dashboard See below for the HTML code for the body tag If the current URL is http://localhost/ang/#/login, then the body should include ...

The parser encountered an unexpected token while attempting to parse the provided string

Struggling to correctly parse a JSON response from a server using node, as it is showing up as a string. Here's an example: "{name:'hello'}" Recreated the issue here: http://jsfiddle.net/x5sup14j/ Tried replace(/'/g, '"'); ...

Turn off the background when the automatic popup box appears

I have implemented a popup box that automatically appears after 5 seconds when the site is loaded. However, if I click outside of the popup box, it closes. I want to disable the background when the popup box is displayed. If I remove the two lines of code ...

Implement a new feature on a jQuery element

I'm currently exploring ways to incorporate a function into an object using jQuery, but unfortunately I am facing unexpected challenges. You can find a minimal example illustrating my issue on this jsfiddle page. I have recently started learning jQue ...

How to Use Attributes as Component Parameters in Vue.js

I am currently developing a test Component using Vue.js. I am trying to pass a parameter to be used in my template like this: Vue.component('test', { props: ['href'], template: '<li><a href="{{href}}"><slot> ...

What is the process for including a new item in the p-breadcrumb list?

Having trouble getting my code to add a new item to the p-breadcrumb list on click. Any assistance would be greatly appreciated. Thank you in advance! Check out the live demo here ngOnInit() { this.items = [ {label: 'Computer'}, ...

Problems encountered when attempting to create a link between two nodes on a force-directed graph using a mouse click

I'm currently working on creating an interactive graph where users can click on two nodes to establish a link between them (which can be removed later). My approach was inspired by Mike Bostock's example at: https://bl.ocks.org/mbostock/1095795 ...

Use $parse to extract the field names that include the dot character

Suppose I have an object with a field that contains a dot character, and I want to parse it using $parse. For instance, the following code currently logs undefined - var getter = $parse('IhaveDot.here'); var context = {"IhaveDot.here": 'Th ...

Configure unique headers for various environments

I am looking to customize headers like "id", "env", and "name" based on different environments in my application. Each environment has a unique set of values for these headers. I am struggling to implement this effectively within my existing code logic. T ...

Effortlessly move and place items across different browser windows or tabs

Created a code snippet for enabling drag and drop functionality of elements within the same window, which is working smoothly. var currentDragElement = null; var draggableElements = document.querySelectorAll('[draggable="true"]'); [].forEach ...

I am having success posting data through a form, however, the fetch API is not functioning as expected for me

Currently, I am working on a React Project and focusing on creating a signup form. Everything seems to be fine when posting form data using the Form, but things get stuck when I try to use onSubmit={handleSubmit} along with fetch APIs such as axios or just ...

Enable users to choose either today's date or a future date by implementing AngularJS UI Bootstrap

I am currently utilizing the ui-bootstrap directive for a datepicker. My goal is to restrict the selection of dates to today's date or any future dates. Below is the HTML code snippet for the datepicker: <div class="input-group"> ...

Need to create a callback within a sequence of events?

Is it possible to create a callback chain like this? Widget.update(...).onUpdate(function(data){ console.log('updated'); }); Here is the current code snippet: var Gateway = {}; Gateway.put = function(url, data, callback) { $.ajax({ ...

Is real-time updating possible with data binding in Polymer and JavaScript?

I have a scenario where I am working with two pages: my-view1 and my-view2. On my-view1, there are two buttons that manipulate data stored in LocalStorage. On my-view2, there are two simple div elements that display the total value and the total value in t ...

Transmit and receive information between Javascript and Node.js through Express framework

Currently, I am utilizing the Express platform along with the Twilio Node.js SMS API and JavaScript to send text messages to my users. However, I am facing an issue in sending data through GET variables on the front-end and capturing those values with node ...

Tips for removing a single item from a list in ReactJS one by one

There seems to be an issue with the deletion functionality in my project. When a user tries to delete a row, sometimes only that specific row is deleted, but other times when there are only two rows left and one is deleted, the data toggles and replaces it ...

Sort the elements within the *ngFor loop according to the category upon clicking the button in Angular

Currently, I have a collection of items that I am iterating through using *ngFor. Above this list, there are category buttons available as shown in the HTML snippet below. My goal is to enable filtering of the list based on the category of the button click ...