Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,968 members, 7,825,024 topics. Date: Sunday, 12 May 2024 at 12:16 AM

Attaching And Reattaching Event Handlers - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Attaching And Reattaching Event Handlers (748 Views)

Email Not Attaching Documents (2) (3) (4)

(1) (Reply)

Attaching And Reattaching Event Handlers by kehers(m): 1:16pm On Aug 25, 2009
Im using event handlers from a framework (prototype to be specific) to attach listeners to certain elements of a page like this:

//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?

(1) (Reply)

Is Someone Trying To Hack My Site Or What? / My Website Redesigned, What Do Y'all Think / Is Sick Submitter Worth Buying?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.