Hi Andrea, Thanks for your quick response, You suggestion is great. so I declare the code I want to use as a function and as you say pass the function to addEvent
Awesome! On Jan 31, 4:08 pm, Andrea Dessì <[email protected]> wrote: > Hi, > > when you add the "click" event to your "a" elements, > remember you are passing a function! > > so you could think it in this way: > > [code] > //declare your fn > var clickFunction = function(e) { > e.stop(); > //code to execute > > }; > > $$('li a').each(function(el){ > el.addEvent('click',*clickFunction*); > }); > > }); > > ///later in the script.... > var del = new Element('a', {'class':'delete sprite', 'title':'delete'+ > player.name}).inject(li); > //code being repeated > el.addEvent('click',*clickFunction*); > > [/code] > > or something like that :) > -- > Andrea > > On Mon, Jan 31, 2011 at 15:00, roark <[email protected]> wrote: > > Hi Everyone, > > > I think I must be writing bad code here. > > > I will load a page with a few li's with links in them. once loaded, I > > add events to the links > > [code] > > $$('li a').each(function(el){ > > el.addEvent('click',function(e){ > > e.stop(); > > //code to execute > > }); > > }); > > [/code] > > > Then when I create an element dynamically I am writing the same code > > again: > > [code] > > var li = new Element('li').inject($('ul.list')); > > var del = new Element('a', {'class':'delete sprite', > > 'title':'delete'+player.name}).inject(li); > > //code being repeated > > el.addEvent('click',function(e){ > > e.stop(); > > //code to execute > > }); > > [/code] > > > It may not look like much but when I send requests the code gets long > > and it seems I'm duplicating code when I add the 'click' event. > > > This seems like bad code to me, but can't think of a way to write it > > any more efficient > > Any input is greatly appreciated!
