Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery on() - is event.delegateTarget the best way to get the main container of on() function?

Tags:

jquery

ajax

I have a HTML similar to this:

<div class="ajax_table_container">
    <table>
        <tr>
            <td><a href="ajax.php?action=delete&id=1" class="delete_element">DELETE</a></td>
        </tr>
        <tr>
            <td><a href="ajax.php?action=delete&id=2" class="delete_element">DELETE</a></td>
        </tr>
    </table>
</div>

And javascript:

$("div.ajax_table_container").on("click", "a.delete_element", function (event) {
    var adr = $(this).attr("href");    
    $(event.delegateTarget).html("TEST");
    return false;
});

In real application I'm reloading contents of the ajax request from the address provided in the link clicked.

My question is though, is the method I'm using the best to get the main div (with class ajax_table_container). I'm talking about the fragment: event.delegateTarget. Is there any better way of getting it in jQuery? (btw. I don't want to use $('div.ajax_table_container') here - there might be a couple of these divs on one page)

FIDDLE

EDIT: After getting some answers and checking google I would like to mark myself as duplicate ;-) Here: How to get parent selector with jquery.on('click'), base on the clicked element

like image 364
Kelu Thatsall Avatar asked Oct 28 '13 12:10

Kelu Thatsall


People also ask

What is event delegateTarget?

The event. delegateTarget property returns the element where the currently-called jQuery event handler was attached. This property is useful for delegated events attached by the on() method, where the event handler is attached at an ancestor of the element being processed. Tip: event. delegateTarget is equal to event.

What does jQuery use the event namespace for?

The event. namespace property in jQuery is used to return the custom namespace whenever the event is triggered. It is used to handle tasks differently depending on the namespace used.

WHAT IS ON event in jQuery?

The on() method attaches one or more event handlers for the selected elements and child elements. As of jQuery version 1.7, the on() method is the new replacement for the bind(), live() and delegate() methods.

Which function supports event bubbling in jQuery?

Event bubbling on( 'click', function( event ) { event. preventDefault(); console. log( this ); }); It binds a click handler to all elements in a document (something you should never do in real code), as well as to the document and window .


2 Answers

If your goal is to get the element on which you hooked the event, it's not only your best option, but nearly your only option.

Another option, which I'm not advocating, is to remember what you used in a variable that the event handler closes over:

var $container = $("div.ajax_table_container");
$container.on("click", "a.delete_element", function (event) {
    var adr = $(this).attr("href");    
    $container.html("TEST");
    return false;
});

But since jQuery gives you that handy delegateTarget, that's what I'd use barring a good reason to use something else. The cost of passing an element reference (as opposed to a selector) into $() isn't large at all, it's likely whatever else you're doing in the handler washes out any overhead it adds.

like image 177
T.J. Crowder Avatar answered Oct 15 '22 01:10

T.J. Crowder


You can use .closest():

$(this).closest("div.ajax_table_container");

This will return the parent of the clicked link.

like image 2
Patsy Issa Avatar answered Oct 15 '22 02:10

Patsy Issa