Categories
JavaScript Answers jQuery

How to List All Bindings of an Element with JavaScript?

Spread the love

Sometimes, we want to list all bindings of an element with JavaScript.

In this article, we’ll look at how to list all bindings of an element with JavaScript.

List All Bindings of an Element with JavaScript

To list all bindings of an element with JavaScript, we can use the _data method.

For instance, if we have the following input:

<input>

Then we can write:

const input = $('input')
input.bind('click', () => {})
input.bind('input', () => {})
$.each($._data($(input)[0], 'events'), (i, e) => {
  console.log(i, e);
});

to get the input with $('input').

Then we add the event handlers with:

input.bind('click', () => {})
input.bind('input', () => {})

Next, we get the event handler bindings with:

$._data($(input)[0], 'events')

Then we use the $.each method to loop through the bindings returned by $._data.

In the callback, i is the event name string and e has the event data,

Conclusion

To list all bindings of an element with JavaScript, we can use the _data method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *