R
RobG
I have a function whose parameter is a reference the element that called it:
function someFunction(el)
{
...
}
The function is assigned to the onclick event of some elements in the
HTML source:
<input ... onclick="someFunction(this);" ...>
and dynamically to others:
elRef.onclick = someFunction;
When someFunction() is called from the statically-assigned onclick, el
is a reference to the element. But when it has been added dynamically,
el will be either undefined (IE) or 'event' (Mozilla et al).
To assign the right value to el, I can test if el exists and if it does,
whether its constructor is some type of HTML element or 'Event'. But IE
doesn't support the constructor property for elements (or have I just
not found it yet?) nor does it understand 'Event' the way Mozilla does.
Anyhow, the following test does the job:
function someFunction(el)
{
var el=(!el || (el.constructor && Event == el.constructor))? this : el;
...
If el is undefined:
- have IE dynamically added event, el is assigned the value of 'this'
If el is defined
- have IE statically added event, el is assigned the value of el
OR
- have Mozilla:
- if el.constructor is Event, have dynamically added event,
el is assigned the value of this
- if el.constructor is not Event, have statically added event,
el is assigned the value of el
It works, but seems long winded and dependent on the vagaries of the two
event models. As a result, I've been using:
var el = (el && el.nodeName)? el : this;
This seems much simpler - if el is defined and has a nodeName, use it.
Otherwise, use this.
Rather than detecting if the function was passed a reference to 'event'
or not, it sees if an element was passed. It works and assigns a
reference to the element whether called from the statically or
dynamically attached function.
Is that reasonable logic, or should the first method be used? Or is
there a better way of doing what I'm after?
Some sample code:
<input type="button" value="Replace onclick" onclick="
replaceOnclick(this);
alert('Old onclick replaced');
">
<script type="text/javascript">
function replaceOnclick(el)
{
// Short version
var el = (el && el.nodeName)? el : this;
// Long version (wrapped for posting)
// var el = ( !el || (el.constructor
&& Event == el.constructor) )? this : el;
el.onclick = replaceOnclick;
alert( el.nodeName + ': new onclick');
}
</script>
function someFunction(el)
{
...
}
The function is assigned to the onclick event of some elements in the
HTML source:
<input ... onclick="someFunction(this);" ...>
and dynamically to others:
elRef.onclick = someFunction;
When someFunction() is called from the statically-assigned onclick, el
is a reference to the element. But when it has been added dynamically,
el will be either undefined (IE) or 'event' (Mozilla et al).
To assign the right value to el, I can test if el exists and if it does,
whether its constructor is some type of HTML element or 'Event'. But IE
doesn't support the constructor property for elements (or have I just
not found it yet?) nor does it understand 'Event' the way Mozilla does.
Anyhow, the following test does the job:
function someFunction(el)
{
var el=(!el || (el.constructor && Event == el.constructor))? this : el;
...
If el is undefined:
- have IE dynamically added event, el is assigned the value of 'this'
If el is defined
- have IE statically added event, el is assigned the value of el
OR
- have Mozilla:
- if el.constructor is Event, have dynamically added event,
el is assigned the value of this
- if el.constructor is not Event, have statically added event,
el is assigned the value of el
It works, but seems long winded and dependent on the vagaries of the two
event models. As a result, I've been using:
var el = (el && el.nodeName)? el : this;
This seems much simpler - if el is defined and has a nodeName, use it.
Otherwise, use this.
Rather than detecting if the function was passed a reference to 'event'
or not, it sees if an element was passed. It works and assigns a
reference to the element whether called from the statically or
dynamically attached function.
Is that reasonable logic, or should the first method be used? Or is
there a better way of doing what I'm after?
Some sample code:
<input type="button" value="Replace onclick" onclick="
replaceOnclick(this);
alert('Old onclick replaced');
">
<script type="text/javascript">
function replaceOnclick(el)
{
// Short version
var el = (el && el.nodeName)? el : this;
// Long version (wrapped for posting)
// var el = ( !el || (el.constructor
&& Event == el.constructor) )? this : el;
el.onclick = replaceOnclick;
alert( el.nodeName + ': new onclick');
}
</script>