Attaching And Reattaching Event Handlers

Welcome. Please Login, Register, Or Activate! 
type your username and password to login
Date: November 24, 2009, 02:02 PM
431861 members and 298840 Topics
Latest Member: solutech
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderators: OmniPotens, yawa-ti-de)  |  Attaching And Reattaching Event Handlers
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Attaching And Reattaching Event Handlers  (Read 92 views)
kehers (m)
Attaching And Reattaching Event Handlers
« on: August 25, 2009, 01:16 PM »

Im using event handlers from a framework (prototype to be specific) to attach listeners to certain elements of a page like this:
Quote
//html
<a href="?default=1" class="default">Set Default</a>
//js
document.observe('dom:loaded', function(){
   //get all elements with d 'default' classname
   for(i = 0;i < document.getElementsByClassName('default').length;i++)
   {
      s = document.getElementsByClassName('default')[i];
      //attach an onclick event
      s.observe('click', function(e){
         //prevent the default action (i.e following the link)
         e.preventDefault();
         //read d link attribute
         ref = this.readAttribute('href');
         //and now, do it the ajax way
         new Ajax.Request('js/ajax/handler.php'+ref, {
            method:'get',
            onSuccess:function(transport){}
         });
      });
   }
});
It works well. Really well. But there is a challenge. Most times, such actions manipulates the DOM and may add one or more of the 'default' class element (i.e another <a href="?default=1" class="default">Set Default</a> somewhere). That is intentional actually. But then, the newly added 'default' class element will not have the click event attached. The shitty work around at the moment is to loop through all the 'default' classes again and re-attach the events. Any other better ideas?
 Saint Michael Press Talks   Reliable And Afordable Web Hosting  Debugging Css  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 


Sections: Autos/Cars (2) Jobs/Vacancies (2) (3) Career Talk Education General(2) Politics Romance Computers Phones Travel
Sports Fashion Health Religion Celebrities TV/Movies (2) Music/Radio (2) Books Webmasters Programming

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa. See also: Nairalist Classified Ads
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.