Inspecting And Reusing jQuery Events

19th July 2020

Adding events to HTML elements with jQuery is pretty simple, but I found that extending those events wasn't an easy task. I was faced with an issue where I had some third party code that performed an action on an element, and I needed to add an event and call the same handler from that event. As this was within a CMS I had limited scope to just add my new event to the existing code, so I needed a way of pulling out the current jQuery events and then calling that event handler separately. This post looks at how I accomplished this.

Let's start with a couple of simple form input elements.

  1. <input type="text" class="field-text" />
  2. <input type="text" class="field-next" />

As an example that can be useful here I'm going to set up a 'change' event that will copy the text entered into field-text into the field-next. This is a simple enough example and doesn't require that much code.

  1. var fieldtext = $(".field-text");
  2. var fieldnext = $(".field-next");
  3.  
  4. var copytext = function() {
  5. fieldnext.val(fieldtext.val());
  6. };
  7.  
  8. fieldtext.on("change", function(event) {
  9. copytext();
  10. });

With the event setup we can then use jQuery to extract the events into a variable. To extract the event data out of the field we need to use a somewhat undocumented function called _data. This function accepts the element and the type of data we are trying to retrieve, in this case we pass 'events' to get the events data.

var events = $._data(fieldtext[0], "events");

The 'events' variable now contains all of the events associated with this element. What I want to add to this was a 'keyup' event so that when the user stopped typing we would wait for 1 second before calling a function called 'doneTyping'. If the user resumes typing we would just restart the counter. This sort of technique is useful in search suggestions or other network related lookups where you need a second in order to retrieve the content. By delaying the lookup slightly a better user experience is created where they aren't seeing things pop in and out whilst they are typing.

  1. var typingTimer;
  2. var doneTypingInterval = 1000;
  3. fieldtext.on("keyup", function(event) {
  4. clearTimeout(typingTimer);
  5. typingTimer = setTimeout(doneTyping, doneTypingInterval, event);
  6. return false;
  7. });

The final step here is to call the original event handler. What we do here is pull up the events list we created before and run the handler function that exists on every change event found. We also pass in the current event parameter to ensure that the event handler is called in the same way that an event handler would call it.

  1. function doneTyping(event) {
  2. $.each(events.change, function() {
  3. this.handler(event);
  4. });
  5. }

This effectively means that the change and keyup events are running the same code, but we have gone about it in a little bit of a circuitous route due to the limitations of the platform.

If you want to see all of this code in action then I have created a CodePen that contains all of the code above.

Add new comment

The content of this field is kept private and will not be shown publicly.