views:

309

answers:

2

Sample code:

<div id="first">first div</div>
<div id="second">second div, click here or on the previous it's the same</div>

Suppose I set an event onclick on the first div that alerts the div's content. How do I call that event? In example clicking on the second div I'd like it to call the click event of the first div and alert('first div')

+3  A: 

Check out their Element.fireEvent() method. Try this:

$('first').addEvent('click', function()  { 
             alert($('first').innerHTML); 
});

$('second').addEvent('click', function()  { 
             $('first').fireEvent('click'); 
       });
Brian Ramsay
A: 

$('first').click();

ifwdev
this is not jquery and jquery is not javascript... sigh.
Dimitar Christoff
that is valid moo code and it is the simplest way to do it, IMO.
ifwdev
no it is not. you can use something like this:Element.implement({'click': function(fn) { return this.addEvent('click',fn);} }); to mimic jquery's syntax but it certainly has not been a part of mootools core thus far...
Dimitar Christoff
oh - and this will just do the assignment and not the calling of the function - for that you need to fireEvent again :)
Dimitar Christoff
I think I need a vacation... I swear that was working in firebug yesterday. I think I element.implemented it in my helpers somewhere.
ifwdev