tags:

views:

1106

answers:

1

I'm trying to use this code to add an anchor element and set it's onclick event to some Javascript. But no event is being loaded. Looking at the DOM through Firebug tells me that Mootools is indeed added. The element has almost all of the Mootools element functions. But it lacks addEvent.

var delete_ctl = new Element('a', 
{
    'href' : '#',
    'events' : 
    {
        'click' : function() { alert('foo'); }
    }   
});


delete_ctl.appendText('Remove');
delete_ctl.inject(root);
delete_ctl.addEvent('click', function() { alert('foo'); });

The rendered code in Firebug lacks any indication that the onclick event has been set. When the code is run, Firebug reports a Javascript error: "addEvent is not a function." I feel like I'm missing something basic here.

+1  A: 

You code works fine on my machine. I get twice alerts of 'foo'.
Make sure you use the right version of mootools.
Make sure root really references an HTML element in the DOM.
Make sure you run this code after the DOM finished loading (onload or domready events).
Good luck.

Itay Moav