Using a glyph library like Font Awesome, buttons often end up looking something like this:
<button>
<span class="fa fa-stack-overflow fa-lg"></span> Click me
</button>
When handling a button event, the target can be either the span or the button itself, depending on where the user clicks, so I normally end up with something like this:
$('button').on('click', function($event) {
var $target = $($event.target);
var $button = $target.is('button') ? $target : $target.closest('button');
// useful stuff goes here
}
Is it possible to extend the jQuery event object to add a method that encapsulates this functionality, so it can be called like this?
var $button = $event.getSpecificTarget('button');
I've created a JSFiddle to demonstrate the issue.
It could be done using a normal jQuery plugin, but it would feel cleaner if it could be accessed straight from the event object, rather than via a plugin like this:
var $button = $.getSpecificTarget($event, 'button');