Get container with on () in jQuery

I have a button inside a div to hide this div, and I want to pass the div to the handler. Here is my current code:

$('#hidden-div').on('click', '#hide-btn', { element : $('#hidden-div') }, hideElement);

Is there a way to avoid reselecting a container? Something like this would be nice:

$('#hidden-div').on('click', '#hide-btn', { element : $(this) }, hideElement);
+5
source share
1 answer

event.delegateTarget contains a reference to the DOM element.

$('#hidden-div').on('click', '#hide-btn', hideElement);

function hideElement(e) {
    $(e.delegateTarget)//do stuff
}

You still have to wrap it inside a jQuery object, but creating jQuery objects from references to DOM elements does not request the DOM.

+6
source

All Articles