views:

23

answers:

1

I don´t know if I have forgotten how to do so or if it´s a bug, but I just can´t find the caller's reference using jQuery.

I´m doing the following:


$(document).ready(function() {
    $('#parent a.item').click(doSomething);
});

function doSomething(e) {
    // Alerts for demostrational purposes only
    alert(e.target);
    alert(e.currentTarget);
    alert(this);
    alert($(this)[0]);
}

All the alerts show the hyperlink´s href attribute(page URL + '#').
Am I doing something wrong?

Notes: Using jQuery 1.4.2.

+2  A: 

It's because you're alerting so you're seeing the string representation (since alert() takes a string)...which for an anchor is the href. You could do this for example:

alert(e.target); //or perhaps alert(this.target); - alerts the href
alert(e.target.innerHTML);  //or perhaps alert(this.innerHTML); - alerts the html

You can try it out/play with it here, note that this and e.target aren't always the same, if the click came from a child element, they'll be different.

Nick Craver
Well, I see that´s been working... but still, what I was originally trying to do was the following: $(e.target).closest('dl'), wich is still failing.
Joel Ferreras
Thanks for the example BTW!
Joel Ferreras
@Joel - Can you post what your markup looks like?
Nick Craver
My bad, the hyperlink was beside the dl, not inside.
Joel Ferreras