Meryovi
Meryovi

Reputation: 6231

jQuery Event.Target for caller's reference

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 in the "click" event 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.

Upvotes: 1

Views: 1401

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

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.

Upvotes: 3

Related Questions