This is not a question about jQuery, but about how jQuery implements such a behaviour.
In jQuery you can do this:
$('#some_link_id').click(function()
{
alert(this.tagName); //displays 'A'
})
could someone explain in general terms (no need you to write code) how do they obtain to pass the event's caller html elments (a link in this specific example) into the this keyword?
I obviously tried to look 1st in jQuery code, but I could not understand one line.
Thanks!
UPDATE: according to Anurag answer I decided to post some code at this point because it seems easier to code than what I thought:
function AddEvent(html_element, event_name, event_function)
{
if(html_element.attachEvent) //IE
html_element.attachEvent("on" + event_name, function() {event_function.call(html_element);});
else if(html_element.addEventListener) //FF
html_element.addEventListener(event_name, event_function, false); //don't need the 'call' trick because in FF everything already works in the right way
}
and then now with a simple call we mimic jQuery behaviour of using this in events handlers
AddEvent(document.getElementById('some_id'), 'click', function()
{
alert(this.tagName); //shows 'A', and it's cross browser: works both IE and FF
});
Do you think there are any errors or something I misunderstood taking the all thing in a too superficial way???