How can I bind all events (i.e. click
, keypress
, mousedown
) on a DOM element, using jQuery, without listing each one out individually?
Example:
$('#some-el').bind('all events', function(e) { console.log(e.type); });
jQuery bind() Method The bind() method attaches one or more event handlers for selected elements, and specifies a function to run when the event occurs.
To bind to an event you use the Angular event binding syntax. This syntax consists of a target event name within parentheses to the left of an equal sign, and a quoted template statement to the right.
You can use the live() method to bind elements (even newly created ones) to events and handlers, like the onclick event. Save this answer.
bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs.
there is a simple (but not accurate) way to test all events:
function getAllEvents(element) { var result = []; for (var key in element) { if (key.indexOf('on') === 0) { result.push(key.slice(2)); } } return result.join(' '); }
then bind all events like this:
var el = $('#some-el'); el.bind(getAllEvents(el[0]), function(e) { /* insert your code */ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With