I'm using this:
$('.sizeChart').on('vclick', '.entry .ui-btn', function(e){
console.log( e )
console.log( e.currentTarget )
console.log( $( e.currentTarget )
console.log( $( e.currentTarget ).find('input.qtyInput') )
var qty = $( e.currentTarget ).find('input.qtyInput');
// do something
});
Which works, but $( e.currentTarget ).find(...)
seems awkward to me.
I can't bind directly to the input
because this binding will go dead on iOS3+4 after a couple of clicks. Binding to the closest ui-btn
works throughout.
Question:
Is there a better/easier/faster way to do the binding than what I'm using ?
Answer: Use the event. target Property You can use the event. target property to get the ID of the element that fired an event in jQuery. This property always refers to the element that triggered the event. The following example will display the name of the element that was just clicked.
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
currentTarget is only available while the event is being handled. If you console. log() the event object, storing it in a variable, and then look for the currentTarget key in the console, its value will be null . Instead, you should either use console.
target is the element that triggered the event (e.g., the user clicked on) currenttarget is the element that the event listener is attached to. Save this answer.
You can just use this
instead of e.currentTarget
:
$(this).find(...);
Proof that event.currentTarget
and this
are the same.
Also the documentation says:
This property will typically be equal to the
this
of the function.
That's about it. It is pretty common to pass a DOM element directly to jQuery and use DOM traversal methods on it.
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