473,406 Members | 2,467 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,406 software developers and data experts.

Multiple eventhandlers on a single event

Hi all,

I'm looking for a way to "bind" multiple eventhandler function to the same
event. In other languages this can often be done by using the += operator,
unfortunately this doesn't seem to work.

My (working) code for assigning an eventhandler for some form elements is:

for( var i = 0; i < document.formx.elements.length; i++ ) {
document.formx.elements[i].readonly = true;
alert( document.formx.elements[i].onclick );
document.formx.elements[i].onclick = HandlerFunctionX;
}

Is this possible? And if so, how should it be accomplished?

Thanks in advance,

Jan-Willem
Jul 23 '05 #1
2 1581
On 18 May 2004 05:50:55 -0700, JeeWee wrote:
I'm looking for a way to "bind" multiple eventhandler function to the same
event. In other languages this can often be done by using the += operator,
In C#, uh?
unfortunately this doesn't seem to work.


You can use addEventListener method (for browsers DOM W3C compliant), and
attachEvent for IE.

Or simply:

object.onclick=function(){
f1();
f2();
f3();
}
--
C'ya,
ZER0 :: coder.gfxer.webDesigner();

...ma in effetti il concetto di WYSYWYG (quello che vedi e' quello che
ottieni)
si applica ai computer malissimo, cosi' come si applica alle donne...
(The Real Programmer)

Now playing: nothing
Jul 23 '05 #2
JeeWee wrote:
Hi all,

I'm looking for a way to "bind" multiple eventhandler function to the same
event. In other languages this can often be done by using the += operator,
unfortunately this doesn't seem to work.

My (working) code for assigning an eventhandler for some form elements is:

for( var i = 0; i < document.formx.elements.length; i++ ) {
document.formx.elements[i].readonly = true;
The property name is "readOnly", not "readonly".
alert( document.formx.elements[i].onclick );
document.formx.elements[i].onclick = HandlerFunctionX;
}

Is this possible? And if so, how should it be accomplished?
I solved this a while back because I had the same requirement. Here is the
solution I came up with:

function appendFormElementEvent(formName, elementName, elementEvent,
eventHandler) {

var f = document.forms[formName];

if (f && f.elements[elementName]) {

f = f.elements[elementName];

if (f[elementEvent]) {
// there is currently an on... event defined for this
// element; append the event handler javascript to the
// current event handler javascript
f[elementEvent] = new Function(
// take the original event
f[elementEvent]
// convert it to a string; the string looks like:
// "...function...()...{...[js]...}..."
.toString()
// remove any newline characters or carriage returns
.replace(/[\r\n]/g, '')
// remove any leading or trailing whitespace
.replace(/\s+$|^\s+/g, '')
// turn "function...()...{...[js]...}"
// into "function...()...{...[js][newline][new js]...}"
// NOTE: THE NEXT LINE IS A SINGLE STATEMENT WHICH WILL
// MOST LIKELY WRAP
.replace(/^function\s*\w+\s*\(\w*\)\s*\{\s*(.*)\s*\}$/, '$1\n' +
eventHandler)
);
} else {
// there is currently no on... event defined for this
// element; set the event handler javascript to handle
// the event
f[elementEvent] = new Function(eventHandler);
}
}
} // appendFormElementEvent()

- formName is a string, the name of the form
- elementName is a string, the name of the element
- elementEvent is a string, the name of the event (onclick, onfocus, etc)
- eventHandler is a string, the new Javascript code to be added to the event

you use it as follows:

appendFormElementEvent('myForm', 'mySelect', 'onchange', 'function1();');
appendFormElementEvent('myForm', 'mySelect', 'onchange',
'alert(this.selectedIndex);');

Works in Geck based browsers (Mozilla, Firefox, Camino), Internet Explorer
5.5+, Netscape 4.78 and Opera 7+. You might be able to get it working in Opera
6, I have no idea why it doesn't, most likely the format of the toString()
returned by the currently defined event.

I'm sure there are other browsers it doesn't work in, it might be possible to
massage the regular expression to handle those browsers if it were really
necessary.
Thanks in advance,

Jan-Willem


--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 23 '05 #3

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

Similar topics

3
by: Catherine Lynn Smith | last post by:
I'm looking through the client side javascript reference and there's some mighty useful information in here, but it is not very specific on 'reading' information from event handlers. In the...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
3
by: Armin | last post by:
Hello I have a UserControl with a Click Event. Is it possible to find out the List of all Delegates/Eventhandlers using the Event. I read something about a "getinvocationlist" Methode for...
1
by: Kasper Birch Olsen | last post by:
Hi NG Im adding a bunch of linkbuttons to a page, in a for loop, but I cant get the eventhandlers to work. A simplyfied version of the code looks like this: for (int i = 0; i<10; i++) {...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.