By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,143 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,143 IT Pros & Developers. It's quick & easy.

Adding multiple functions to an existing funtion in an event handler

Expert 100+
P: 392
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
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 392
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

Expert 100+
P: 392
Ok never mind the never mind it is not working after all.


any ideas?
Mar 8 '07 #3

100+
P: 428
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
Expert Mod 15k+
P: 16,027
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
Expert 100+
P: 202
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
Expert Mod 15k+
P: 16,027
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

Expert 100+
P: 392
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

Post your reply

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