'How simulate an event that needs to be caught by an Event.observe?

I have a function controlling the content of an element. This function is called on each blur event, using an Event.observe.

I tried to use $(id_element).blur(); but the event was not caught by the Event.observe.

Do you have some advice? How should I simulate the blur event so it is caught by the Event.observe?



Solution 1:[1]

If you have to catch the blur event when it's fired try this :

$("body").on("blur", "#"+id_element, function(){
    //Do something
});

If you want to simulate the blur event see the .trigger() method.

Solution 2:[2]

I have created a dummy example using JSFiddle, it might be the case that you are triggering event before declaring the event.observer...

Observer

HTML

<button id='myElement'>Trigger window load</button>
<i>Have a look at your console.</i> 

JS

$('myElement').observe('click', function(){alert('aaa');});
$('myElement').click();

Hope this helps you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kevin Grosgojat
Solution 2 Nitin Garg