473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Assigning same onclick dynamically and statically


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>


--
Rob
Oct 17 '05 #1
4 2240


RobG wrote:

<input ... onclick="someFunction(this);" ...>

and dynamically to others:

elRef.onclick = someFunction;


You need
elRef.onclick = function (evt) {
someFunction(this);
};
then to have the same as that onclick attribute in the HTML markup creates.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 17 '05 #2
VK

RobG wrote:
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.


Another (clearer??) way:

function someFunction() {
var el = someFunction.arguments[1] ||
someFunction.arguments[0].target;
...
}

<input ... onclick="someFunction(null,this);" ...>

elRef.onclick = someFunction;

I say that Mozilla/3W way demostrates its big flaw here: it leads to
the left/right arguments' shift in functions depending on how they are
being called: from the program stream or by event listeners. That's a
pain for distributed libraries.

Oct 18 '05 #3
VK wrote:
RobG wrote:
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.

Another (clearer??) way:

function someFunction() {
var el = someFunction.arguments[1] ||
someFunction.arguments[0].target;
...
}

<input ... onclick="someFunction(null,this);" ...>

elRef.onclick = someFunction;


I don't think that's an improvement - it introduces peculiarities at
both ends.

I say that Mozilla/3W way demostrates its big flaw here: it leads to
the left/right arguments' shift in functions depending on how they are
being called: from the program stream or by event listeners. That's a
pain for distributed libraries.


The 'flaw', if there is one, is that there are two models - I can't
comment on whether one is better than the other.

Martin's suggestion is fine, I've used it frequently but it seems a bit
of a hack to use an anonymous function to call another function (I guess
views on that differ).

Incidentally, nice you see you're back contributing :-)
--
Rob
Oct 18 '05 #4
VK
> VK wrote:
... it leads to the left/right arguments' shift in functions depending on...
Correcting myself as a wrong wording and explanation: there is not any
*shift* here. The first argument in each function (3W model) is
reserved for the Event data. You can use it for other purposes but it
will be like sitting on someone place ready to leave at any moment. As
soon as your function is called by an event listener, the first
argument will contain the Event data no matter what kind of argument
your function was really expecting. We may call it "a different
approach" or "an unforseen oops of the chosen model", it's really a
question of preferences :-)
So wrappers from anonymous functions (despite their semi-ugliness)
would be a choice I guess.

the following test does the job:
function someFunction(el)
{
var el=(!el || (el.constructor && Event == el.constructor))? this : el;
There must be some surrounding code missing because here (and in the
other variant) you assign window object to el in IE case (this ==
window.self within named function body). That doesn't seem your aim.

Incidentally, nice you see you're back contributing :-)

;-)

Oct 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
1
by: David J. Berman | last post by:
Thanks for any help...! My error is: Object reference not set to an instance of an object. > public int DisplayOrder { > get { >>>>>> return (int) ViewState; > }
5
by: Tim Mulholland | last post by:
I am trying to create an ASPX page that, based on QueryString inputs, returns another file, in this case a WMA file. I do not want to do this by redirect, because these files are not available...
4
by: puzzlecracker | last post by:
I have seen these terms used in Gang of 4 but could never thoroughly understand what it meant I perceive that C++ is both, but apparently it is only a latter. Can someone explain it? thx
4
by: Cookie Jar | last post by:
I have a situation where I am trying to abstract away not only the organization of the presentation (via TPL files) but also some of the finer formatting features (via CSS). So, for a particular...
7
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
5
by: Chris | last post by:
I have a page with mixture of static and dynamically added controls is there any way of controlling the order which they are added to the page. My submit button (statically added) appears before...
36
by: Martin Larsen | last post by:
Hi, When a PHP program links to a library using include or require (or their _once variations), is the library then linked dynamically or statically? While it might seem irrelevant from a...
1
by: bambr | last post by:
I'm using following code: function cloneRow(tBodyId, sourceRowId) { var obj = document.getElementById(tBodyId); if(obj.tagName != 'TBODY') { for(var i = 0; i < obj.childNodes.length;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.