Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select/find an element inside an event.currentTarget in Jquery?

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 ?

like image 560
frequent Avatar asked Nov 14 '12 11:11

frequent


People also ask

How do you find the elements of an event?

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.

How do you target an element ID in jQuery?

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.

Can I use event currentTarget?

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.

What is the difference between event target and event currentTarget?

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.


1 Answers

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.

like image 104
Felix Kling Avatar answered Oct 19 '22 22:10

Felix Kling