473,394 Members | 1,739 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,394 software developers and data experts.

Adding multiple functions to an existing funtion in an event handler

392 Expert 256MB
Does any one know how to add a function to an event handler with out losing the current event handler?

For example you can add multiple functions to a single event handler with something like :

Expand|Select|Wrap|Line Numbers
  1. var someEle = document.getElementById( 'someId' );
  2. if( someEle == null ) { return false; }
  3. someEle.onmouseover = new Function(" someFunction1(); someFunction2(); ");
But if you do not know what if you do not know what someFunction1() is can you still add someFunction2() to this event handler without losing its existing function call(s)?


Some thing like

Expand|Select|Wrap|Line Numbers
  1. var someEle = document.getElementById( 'someId' );
  2. if( someEle == null ) { return false; }
  3. someEle.onmouseover = someEle.onmouseover + new Function(" someFunction2(); ");
I can not find anything that works.
Mar 8 '07 #1
7 2432
pronerd
392 Expert 256MB
DOH!!!! Never mind. What I had was working. I was making a really stupid brain dead mistake and it was never being called.
Mar 8 '07 #2
pronerd
392 Expert 256MB
Ok never mind the never mind it is not working after all.


any ideas?
Mar 8 '07 #3
mrhoo
428 256MB
Expand|Select|Wrap|Line Numbers
  1. function addEvent(hoo,wot,fun){
  2.     if(hoo.attachEvent) hoo.attachEvent('on'+ wot,fun);
  3.     else if(hoo.addEventListener){
  4.         hoo.addEventListener(wot,fun,false);
  5.     }
  6. }
addEvent(window,'load',somefunction);
addEvent(window,'load',someotherfunction);
or
addEvent(anyelement,'click',somefunction);
addEvent(anyelement,'click',someotherfunction);
addEvent(anyelement,'click',andyetanotherfunction) ;

All the functions are called on the selected event type for the window or element
Mar 9 '07 #4
acoder
16,027 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. function addEvent(hoo,wot,fun){
  2.     if(hoo.attachEvent) hoo.attachEvent('on'+ wot,fun);
  3.     else if(hoo.addEventListener){
  4.         hoo.addEventListener(wot,fun,false);
  5.     }
  6. }
That's a useful function. Gets rid of the browser incompatibilities in a neat way.

If you want to see this explained in more detail, see this page.
Mar 9 '07 #5
sumittyagi
202 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. function addEvent(hoo,wot,fun){
  2.     if(hoo.attachEvent) hoo.attachEvent('on'+ wot,fun);
  3.     else if(hoo.addEventListener){
  4.         hoo.addEventListener(wot,fun,false);
  5.     }
  6. }
addEvent(window,'load',somefunction);
addEvent(window,'load',someotherfunction);
or
addEvent(anyelement,'click',somefunction);
addEvent(anyelement,'click',someotherfunction);
addEvent(anyelement,'click',andyetanotherfunction) ;

All the functions are called on the selected event type for the window or element
I am confused here.
can we attach more than one function to a given event of a given element at the same time. I think it is not possible. (although calling a function from other function is a different issue).

I think here:
addEvent(window,'load',somefunction);
addEvent(window,'load',someotherfunction);

second line will attach someotherfunction to load event, and somefunction will be replaced.

any comments?
Mar 9 '07 #6
acoder
16,027 Expert Mod 8TB
I am confused here.
can we attach more than one function to a given event of a given element at the same time. I think it is not possible. (although calling a function from other function is a different issue).

I think here:
addEvent(window,'load',somefunction);
addEvent(window,'load',someotherfunction);

second line will attach someotherfunction to load event, and somefunction will be replaced.

any comments?
See the link that I gave above. You can do exactly that - attach more than one event to an element.
Mar 9 '07 #7
pronerd
392 Expert 256MB
addEvent(window,'load',somefunction);
addEvent(window,'load',someotherfunction);
or
addEvent(anyelement,'click',somefunction);
addEvent(anyelement,'click',someotherfunction);
addEvent(anyelement,'click',andyetanotherfunction) ;
Thanks!!!!
Mar 9 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like...
4
by: WebBuilder451 | last post by:
I'm trying to add an event handler to the on click event of an existing link button. here is the code: Dim thscc As LinkButton dWrk = dtrCal("calDate") thscc.onClick = New EventHandler(AddressOf...
5
by: Chris Lieb | last post by:
I am trying to add an event listener to the keyup event for some text inputs that I am creating dynamically. I have no problems getting it to work in Firefox, but IE just ignores them. I have...
5
by: J | last post by:
I am having problems dynamically adding more than one event handler to an input. I have tried the Javascript included at the bottom. The lines inp.attachEvent('onkeyup',...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
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:
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.