I currently use the following code to trigger an event from my Firefox Add-On.
var hiddenArea = document.getElementById("section_help");
if (hiddenArea) {
var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
hiddenArea.dispatchEvent(evt);
}
I tried to achieve the same with jQuery using
$("#section_help").trigger("click");
but it doesn’t work.
jQuery doesn’t trigger the event that was embedded by the original code.
Where is my fault?
The jQuery documentation mentions the following:
It’s not really clear what they mean by that so I had to look at the source code. Apparently, jQuery doesn’t really create a native event object. It will merely trigger its own event handlers (the ones added via jQuery) and whatever it can find in
onfooproperties. Event handlers added viaaddEventListener()won’t be triggered. Native actions associated with the event won’t be triggered (jQuery seems to be handling a few special cases however).To sum up: better stick with your old way of creating events. It might be more verbose but it does what you expect.