0

Let's say I have an element on a web page created via a dojo widget that has events attached using dojoAttachEvent that looks like so:

<span id="menuItemIWantToTrigger" dojoattachevent="onMouseOver: onHover; onMouseOut: onUnhover; onClick: _onClick;" class="dojoMenuItem2" style="-moz-user-select: none;" dojoinsertionindex="4">Workflow... </span>

I need a click on another element on the page to trigger the onClick event on the widget element. In Firefox 5, I can remotely trigger the event using the plain old JavaScript .click() method, like so:

document.getElementById('menuItemIWantToTrigger').click();

In other browsers (like Firefox 3.6 and 4), that method doesn't work. It seems that those browsers don't pass the triggered click event onto the widget's dojoAttachEvent handlers, but Firefox 5 (and, weirdly, IE 7) do; in fact, those browsers seem to handle dojoAttachEvents exactly like plain old DOM events. So, is there any way I can trigger the onClick dojoAttachEvent in all browsers the same way I can trigger it in Firefox 5?

An important note: I don't have access to the code that's creating the dojo widget elements, so I can't rewrite the way events are bound.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Niq
  • 3
  • 1
  • 2
  • If I recall correctly, dojoAttachEvent is only used in templates. `menuItemIWantToTrigger` doesn't seem to be in a template. The wierd thing is that it works at all for some browsers. I'll probably need to dig through the Dojo sources to check, because dojoAttachEvent should not do anything in this situation... – Stephen Chung Jul 15 '11 at 02:42
  • And JavaScript is case-sensitive. Have you tried "onmouseover", "onclick" etc. instead of the mixed-casing ones? See link on dojoAttachEvents: https://docs.google.com/Doc?docid=dw2zgx2_325c42tg8dx&hl=en&pli=1 – Stephen Chung Jul 15 '11 at 02:43

1 Answers1

2

You need to programatically create an Event object and invoke the event dispatcher on the document. Unfortunately, how you do that is browser dependent.

See this question: How to generate a right-click event in all browsers and this answer (to another question) for a generic solution on event dispatching.

Another, cleaner and better solution, would be to invoke the handler function directly. But I don't know if you have access to it.

Community
  • 1
  • 1
Gustavo Giráldez
  • 2,610
  • 18
  • 12
  • The only way I could solve this problem consistently across all browsers was to do some digging and invoke the function called by the handler function directly (basically, the "_onclick" handler function called another function which called another function--that last function is the one I wanted to invoke with my triggered click). Thanks for the advice! – Niq Jul 18 '11 at 12:32