Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,036,696 members, 7,447,677 topics. Date: Thursday, 01 June 2023 at 10:56 PM

Attaching And Reattaching Event Handlers - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Attaching And Reattaching Event Handlers (711 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:

<a href="?default=1" class="default">Set Default</a>
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)
//read d link attribute
ref = this.readAttribute('href');
//and now, do it the ajax way
new Ajax.Request('js/ajax/handler.php'+ref, {
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)

Software Available / Is There Any Character That Create------------------------- / Introducing Flymyads.com. Be Seen, Be Heard, Be Paid !

(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 - 2023 Oluwaseun Osewa. All rights reserved. See How To Advertise. 15
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.