5

I have a button inside a div to hide that 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 any way to avoid reselecting the container? Something like this would be nice:

$('#hidden-div').on('click', '#hide-btn', { element : $(this) }, hideElement);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Lim H.
  • 9,870
  • 9
  • 48
  • 74
  • possible duplicate of [Getting the element that a delegated event is bound to - jQuery](http://stackoverflow.com/questions/8244350/getting-the-element-that-a-delegated-event-is-bound-to-jquery) – Felix Kling Feb 02 '13 at 14:39
  • @FelixKling: this is the second time you busted me and justly because the question you posted is exactly what I wanted :) Due to my limited jQuery vocabulary at the moment, I just couldn't form the correct search query. Please help me close this question. – Lim H. Feb 02 '13 at 14:42
  • Oh is it? :) Don't worry, it's only logical that the more questions exist, more and more will be duplicates. And yes, one of the biggest problems is that you cannot find a solution/question, when you don't know what you have to look for ;) – Felix Kling Feb 02 '13 at 14:45

1 Answers1

6

event.delegateTarget holds a DOM element reference.

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

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

You'd still have to wrap it inside a jQuery object, but creating jQuery objects from DOM element references does not query the DOM.

Fabrício Matté
  • 69,329
  • 26
  • 129
  • 166