click on an element across browsers


 / Published in: JavaScript
 

If you have assigned an event handler to a container, you might want to remotely trigger it as if a child of the container had been clicked. That is, you might want to manually set the target of the 'event' object that is passed to the event handler on the container. This helps to keep the number of event handlers down.

  1. //assuming that 'menu' has an appropriate onclick handler:
  2. function simulateClick( itemToClick, menu) {
  3. if ( ! itemToClick.click ) {
  4. //== Non-IE:
  5. menu.onclick({target: itemToClick});
  6. } else {
  7. //== IE:
  8. itemToClick.click()
  9. }
  10. }

Report this snippet  

You need to login to post a comment.