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

Event Listener Fires Twice

Claus Mygind
100+
P: 571
I am baffled as to why an event listener is firing twice on me.

I have two apps that share the same piece of code. Each app has two search buttons, So a total of 4 search buttons. Each executing the same code with different parameters. Of the 4 buttons only one fails by firing twice on a single click.

Here are the buttons as coded in the two apps:
Expand|Select|Wrap|Line Numbers
  1. from App1:
  2. <input 
  3.     type="button"         
  4.     id  ="sbReqClient"         
  5.     value="Change Client"         
  6.     class="InputText"
  7.     tabindex="-1" 
  8. />
  9. <input 
  10.     type="button"         
  11.     id  ="sbComClient"         
  12.     value="Change Client"         
  13.     class="InputText"
  14.     tabindex="-1" 
  15. />
  16.  
  17. from App2:
  18. <input 
  19.     type="button"         
  20.     id  ="sbReqClient"         
  21.     value="Change Client"         
  22.     class="InputText"
  23.     tabindex="-1" 
  24. />
  25. This button when clicked fires twice
  26. <input 
  27.     type="button"         
  28.     id  ="sbComClient"         
  29.     value="Change Client"         
  30.     class="InputText"
  31.     tabindex="-1" 
  32. />
  33.  

Each button is assigned an event listener like so. I copied this code from the one app to the other app, so the code is identical.
Expand|Select|Wrap|Line Numbers
  1.  
  2. For button 2 in app 2 this eventlistener fires twice.
  3.     g.sbComClient = document.getElementById('sbComClient');
  4.     if (g.sbComClient.addEventListener)
  5.     {
  6.         g.sbComClient.addEventListener("mousedown",
  7.                                         function(evt)
  8.                                         {
  9.                                             smallSearch('Client Id','10','8','CLIENT','ID','ComClient','Yes','startSmallSearch');
  10.                                         },
  11.                                     false)
  12.     }
  13.  
  14.     g.sbReqClient = document.getElementById('sbReqClient');
  15.     if (g.sbReqClient.addEventListener)
  16.     {
  17.         g.sbReqClient.addEventListener("mousedown",
  18.                                         function(evt)
  19.                                         {
  20.                                             smallSearch('Client Id','10','8','CLIENT','ID','ReqClient','Yes','startSmallSearch');
  21.                                         },
  22.                                     false)
  23.     }
  24.  
  25. Here is code from app1 which fires properly so you can see all the code involved
  26.     g.sbComClient = document.getElementById('sbComClient');
  27.     if (g.sbComClient.addEventListener)
  28.     {
  29.         g.sbComClient.addEventListener("mousedown",
  30.                                         function(evt)
  31.                                         {
  32.                                             smallSearch('Client Id','10','8','CLIENT','ID','ComClient','Yes','startSmallSearch');
  33.                                         },
  34.                                     false)
  35.     }
  36.  
  37.     g.sbReqClient = document.getElementById('sbReqClient');
  38.     if (g.sbReqClient.addEventListener)
  39.     {
  40.         g.sbReqClient.addEventListener("mousedown",
  41.                                         function(evt)
  42.                                         {
  43.                                             smallSearch('Client Id','10','8','CLIENT','ID','ReqClient','Yes','startSmallSearch');
  44.                                         },
  45.                                     false)
  46.     }
  47.  
  48.  
  49.  
From this point forward all 4 buttons run the exact same code from linked files.

Where should I be looking for the cause of the problem?
What are some possible explanations for multiple firings? I understand double click and that is not the problem, I have checked that.
Dec 2 '11 #1

✓ answered by Claus Mygind

I found the error. The one button firing twice had the event listener defined twice. Too much copying of the code. Well there goes 2 hours of my life that could have been spent better.

Share this Question
Share on Google+
1 Reply


Claus Mygind
100+
P: 571
I found the error. The one button firing twice had the event listener defined twice. Too much copying of the code. Well there goes 2 hours of my life that could have been spent better.
Dec 2 '11 #2

Post your reply

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