Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: pp.koch at gmail.com (Peter-Paul Koch)
- Subject: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Date: Wed Oct 25 13:10:02 2006
> >On 10/25/06, Paul Novitski <paul@xxxxxxxxxxxxxxxxxxx> wrote:
> >> // now act on the object whose behavior triggered
> >>this function
> >> var sClass = this.tagName; // etc.
> >
> >This is incorrect. In IE, events are executed in the window scope, so
> >when you do this.tagName, it is equivalent to doing window.tagName.
> >You must get the target element from the event itself
>
>
> According to PP, Matt, what you say is true if you're inserting your
> function calls in the HTML but I'm correct if applying the behavior
> through the DOM.
In the quoted function the this keyword refers to oSomething; ie. the
element you registered the event handler on.
<tag id="something">something to click</tag>
...
var oSomething = document.getElementById("something");
if (!oSomething) return;
oSomething.onclick = jsDoSomething; <-- event handler
registered on oSomething with the traditional model, in which this
works fine
...
function jsDoSomething(evt)
{
// cancel event-bubbling
if (evt) { event = evt; }
event.cancelBubble = true;
// now act on the object whose behavior triggered
this function
var sClass = this.tagName; // etc. <-- this
refers to oSomething
}
--
-------------------------------------------------------------------
ppk, freelance web developer
http://www.quirksmode.org/
------------------------------------------------------------------
- References:
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Nathan V. Patwardhan
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Matt Warden
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Nathan V. Patwardhan
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Paul Novitski
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Matt Warden
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- From: Paul Novitski
- [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Prev by Date: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Next by Date: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Previous by thread: [Javascript] Problem w/Javascript, interpolation, functions, onclick
- Next by thread: [Javascript] Changing a Combobox content
- Index(es):