bangin 'my head is against it and it starts to hurt.
I am having trouble adding an event to an element. I can add an event and then immediately fire it using element.fireEvent ('click'), but as soon as the element is attached to the DOM, it does not respond to a click.
code example:
var el = new Element('strong').setStyle('cursor','pointer'); el.addEvent('click',function () { alert('hi!'); }); el.replaces(old_element);
however, as soon as I attach this to the DOM, the element does not respond to a click. stick styles (the cursor is a pointer when you hover over the mouse), but no event fires. I tried the mouse manipulator, but to no avail.
any clues here? Did I miss something? I do this everywhere, but in this case it does not work.
EDIT ----------------
ok here is another code. Unfortunately, I cannot expose the real code, as this is for a project that is still under tight wrappers.
basically, all nodes are perceived as "replaceable", then the json found in the rel = "" attribute creates the basis for why it should be replaced. In this particular case, the replaced item is the username that should cause some information when clicked.
again, if I fire the event immediately after adding it, everything is fine, but the element does not respond to a click after attaching it.
HTML -----------
<p>Example: <span class='_mootpl_' rel="{'text':'foo','tag':'strong','event':'click','action':'MyAction','params':{'var1': 'val1','var2': 'val2'}}"></span></p>
JAVASCRIPT ----- assumptions: 1. The two functions below are part of a larger class 2. ROOTELEMENT is set during initialization () 3. MyAction is determined before parsing occurs (and correctly processed in the .fireEvent () test).
parseTemplate: function() { this.ROOTELEMENT.getElements('span._mootpl_').each(function(el) { var _c = JSON.decode(el.get('rel')); var new_el = this.get_replace_element(_c); // sets up the base element if (_c.hasOwnProperty('event')) { new_el = this.attach_event(new_el, _c); } }); }, attach_event: function(el, _c) { el.store(_c.event+'-action',_c.action); el.store('params',_c.params); el.addEvent(_c.event, function() { eval(this.retrieve('click-action') + '(this);'); }).setStyle('cursor','pointer'); return el; },