event.target jquery on() mousedown/up is not giving the dom specified by selector

Nik So picture Nik So · Mar 23, 2012 · Viewed 21.9k times · Source

I have this simple html:

<div class="wrapper">
    <span class="higher">
        [higher]
        <span class="lower">
            hello
        </span>
    </span>
</div>​

and this js:

$('.wrapper').on('mousedown', '.higher', function(e){
    alert($(e.target).attr('class'))
    })​

Basically, when I click on the "hello" word, I am getting the "lower" instead of "higher". How can I make it so that the event's target is the same as specified in the selector parameter of the on()?

Here's a little fiddle for this http://jsfiddle.net/TUvB7/2/

Answer

Simon Edstr&#246;m picture Simon Edström · Mar 23, 2012

Use this insteed, e is the element that started the bubbling

$('.wrapper').on('mousedown', '.higher', function(e){
    alert($(this).attr('class')) // Changed line
})​

http://jsfiddle.net/TUvB7/7/