How to get the original selector using jQuery on () function with filter?

I am using the jQuery function .on()to attach some behavior to an event clickfor an object, for example span.

My setup looks something like this:

$('#container').on('click', 'span', function() {
    // do stuff
});

Inside this function thisis span. How do i get it #container?


Full example: http://jsfiddle.net/aymansafadi/Nk3p9/

<div id="container">
    <span>Click Me!</span>
</div>​

-

$('#container').on('click', 'span', function() {

    var span = $(this),
        div  = false; // This is what I need

    console.log(span);
});​
+5
source share
2 answers

Use event.delegateTarget

$('#container').on('click', 'span', function(e) {

    var span = $(this),
    div  = e.delegateTarget;

    console.log(div);
});

Demo

+8
source

The easiest way is to just use it span.parent().

-1
source

All Articles