469,609 Members | 1,107 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

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 2281
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

Post your reply

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

Similar topics

2 posts views Thread by laredotornado | last post: by
3 posts views Thread by Jim Heavey | last post: by
4 posts views Thread by WebBuilder451 | last post: by
1 post views Thread by The Eclectic Electric | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.