Dealing with JavaScript Events

I have developed an amazing plugin called https://github.com/suprMax/Zepto-onPress

Everything works perfectly except for one small issue. When I receive a callback function, I need to store it along with my actual event handler so that I can detach it when someone attempts to remove the event handler and provide me with the original callback. Essentially, I need the ability to store multiple key-value pairs per element where the key is supposed to be a function and the value is also a function. I tried to implement this functionality, but currently the script internally generates this:

(function(){}).toString()

This method is not ideal as it may result in removing incorrect event handlers due to :

(function(){}).toString() === (function(){}).toString(). 

I believe there must be a better approach to achieve this. Any suggestions would be greatly appreciated.

Answer №1

The most efficient and secure method involves storing your functions within an array, where you can use the indexOf function to search based on pointer values. Utilizing the array's index, you can easily push one method onto another.

var funcArray1 = [];
var funcArray2 = [];

function addFunctions(func1, func2) {
   var index;
   
   if (index = funcArray1.indexOf(func1) > -1) {
     removeFunction(funcArray2[index]);
   } 
   
   index = funcArray1.push(func1) - 1;
   funcArray2[index] = func2;   

}

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

When using AngularJS services to pass data, the data may be lost when the page is refreshed

I'm facing an issue with transferring data from controller A to controller B using a Factory (or a Service) when the user refreshes the browser. I am able to successfully set the data in controller A and retrieve it in controller B, but upon refreshin ...

How can I retrieve the checked values of checkboxes and store them in an array using React?

https://i.sstatic.net/jTRRU.png This toggle button is dynamically generated based on data, and I need to save the checked values in an array. Code {this.state.customersmsselect.map((e, key) => { if(e.isactive == "ON"){ return ( &l ...

Submitting forms with CakePHP and AJAX

I'm struggling with implementing ajax and jQuery functions. I need to update my database table based on checkbox values. Below is the code snippet from a ctp file: $(".chk").click(function(e){ e.preventDefault(); var id = $(this ...

Updating the style of a div in ReactJS with instant changes

Currently working on a project where I need to create a self-centering, self-sizing content editable div. The text is centered and the width is already set to 100% of the parent container; my main concern now is the height. I have implemented a function t ...

What is the best way to retrieve an object from a POST request using Angular AJAX calls in a NODEJS environment?

When the button is clicked, a method will be called. The code for this is as follows: .controller('templeDetailsList', function ($scope, $http, $ionicModal) { $scope.starclick = function(){ var newFav = [{ ...

Display several modal pop ups using AngularJS

I'm currently using AngularJS and I have a requirement where I need to check if a student ID exists in the database when a user clicks a button. If the student ID is found in the database, I want to display #modal1, otherwise show #modal2. Is it achie ...

Acquire the text within an anchor tag using JavaScript

Is it possible to invoke a search for all links on my Wordpress blog? I'm not sure if my idea is correct. Currently, I am using an Ajax call for another search on this site. How can I extract the text from a hyperlink HTML tag? For example: <a href ...

Looking for a way to combine two strings and then search for them in my Next.js React JSX? Also, seeking tips on how to make file content in MDX JS searchable

I'm currently working on a function that searches for specific terms in the 'title' and 'summary' fields entered in a search bar. However, I encountered an error stating that 'const' is not allowed in this context. import ...

Text located in the bottom right corner of the window with the use of JavaScript

Is there a way to replace text at the bottom right of the window, as opposed to the page, so that it remains fixed in the corner of the window? I'm looking to create a "share" link that is always visible. ...

Is there a way to modify the HTTP response code of a PHP page based on a specific condition determined by the response of an AJAX call in jQuery?

When calling a page on my search page using ajax, I receive JSON data. I am trying to set the HTTP response code of the page based on the results. If the keyword is found, I want to keep the default response code. However, if it is not found, I want to cha ...

Ways to display a GIF image as a pop-up during data loading in an MVC application

I am working on a project using the MVC framework. The application retrieves a large amount of data during loading. I would like to display a loading image - a .gif file while fetching records. Below is the code snippet I am currently using: //Loads rec ...

Am I causing my entire React component to re-render needlessly every time the state changes?

I've been attempting to develop an accordion component using React, but I seem to be encountering issues with the animation not functioning as expected. The approach I'm taking is quite standard - setting a max-height of 0 for each item body and ...

Combining and adding arrays that share the same key

Currently, I am working with a for loop that extracts data for each user from the matchlistResponsestats object. Once the for loop completes its iterations, I end up with approximately 90 arrays in this format: ["username", kills, assists, deaths] My goal ...

Incorporate Aria Label into a Website Link

Currently working on enhancing website accessibility. I have identified a close menu button that lacks an Aria Label, and my goal is to rectify this using JavaScript. Although I am utilizing the script below to target the specific ID and add the attribute ...

Seeking out information on our family's ancestry

I am currently developing a Family Tree feature for my web application using HTML5 specifications. Despite researching and coming across various JS samples, none seem to meet my specific requirements. I have explored options like JIT, Rafael, and GoJS, but ...

Retrieve the red, green, and blue components of a color in the RGB format

When I retrieve "rgb(18, 115, 224)" from a DOM element, I need to convert this color to hexadecimal in order to assign it to a span element. To get the hexadecimal equivalent of the color, I can use the following code snippet: "#" + componentToHex(r) + co ...

Is there a method available to incorporate a scroller into an nvd3 chart?

I am encountering an issue with my nvd3 chart. When I have a large amount of data that exceeds the width of the chart container, there is no scroll bar present and I'm struggling to figure out how to add one. I attempted to include overflow:scroll wi ...

Cease the form submission process using Ajax when the input field is blank

I have implemented an ajax code that halts the form submission process if any input value is empty and displays a popup alert. However, I am facing an issue where the process continues even after closing the alert popup. How can I ensure that the process ...

What can I use instead of "display:none" in Javascript or Jquery?

A problem I encountered with my website involves a menu that toggles content visibility based on menu item clicks. The JQuery script responsible for this behavior is shown below: $(document).ready(function() { $("#bioLink").click(function(){ $ ...

Having trouble navigating with router.navigate and utilizing local storage?

I have a code that utilizes router.navigate to direct the user to a specific location abrirLista(categoria, id) { localStorage.setItem('categName', JSON.stringify(categoria)); const slug = slugify(categoria); this.router.navigate(['/lista&ap ...