B
bayfaulkscatering
I'm definitely not new to JS, but for the life of me, I can't figure
this one out. Here's basically what I'm doing:
function foo() {
alert(this);
}
span = document.createElement('span');
a = document.createElement('a');
// Code to add text, etc to the tag here...
a.onclick = foo;
span.appendChild(a);
// ... stuff ...
divObject.appendChild(span);
Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window. I've also tried adding (e) as
a parameter to foo to capture the event, but with no luck. And I tried
using an anonymous function:
a.onclick = function() { alert(this); }
I was thinking maybe it was just giving me the URL was because the a
object was not appended to anything, ergo belonging to the document,
but it seems like the 'this' reference shouldn't even matter until the
function is called, at which time it is set to the a object. Can anyone
help?
this one out. Here's basically what I'm doing:
function foo() {
alert(this);
}
span = document.createElement('span');
a = document.createElement('a');
// Code to add text, etc to the tag here...
a.onclick = foo;
span.appendChild(a);
// ... stuff ...
divObject.appendChild(span);
Instead of seeing like [object HTMLElement] or whatever, I simply see
the URL of the page in the alert window. I've also tried adding (e) as
a parameter to foo to capture the event, but with no luck. And I tried
using an anonymous function:
a.onclick = function() { alert(this); }
I was thinking maybe it was just giving me the URL was because the a
object was not appended to anything, ergo belonging to the document,
but it seems like the 'this' reference shouldn't even matter until the
function is called, at which time it is set to the a object. Can anyone
help?