6

It seems that jQuery's trigger() only runs event handlers that were bound with jQuery. I have some modules that use native browser event binding. Using the code from https://stackoverflow.com/a/2676527 works for me, but I'm wondering if there's something built in to jQuery that will do this?

Update from comments: apparently it works for click events on buttons. But not for change events on select boxes: http://jsfiddle.net/qxpXV/2/

For the record: hacking the other library to do its bindings with jQuery does make trigger() work, but I don't really want to do that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
singpolyma
  • 10,999
  • 5
  • 47
  • 71
  • 1
    @singpolyma What do you mean by native browser event binding? `.addEventListener()`? I am able to invoke such a handler: http://jsfiddle.net/mnrUt/ Hm, even if I specify the code in via an `onevent` HTML attribute, jQuery's `.trigger()` is still able to run that code: http://jsfiddle.net/mnrUt/1/ – Šime Vidas Oct 02 '12 at 21:12
  • @adeneo See my two demos above. – Šime Vidas Oct 02 '12 at 21:15
  • @ŠimeVidas - Seems it does work, always expected it to not work as the docs says it's for events bound with jQuery, and never really got around to test it ! – adeneo Oct 02 '12 at 21:42
  • @ŠimeVidas interesting that it works for click. But not for change on a select box: http://jsfiddle.net/qxpXV/2/ – singpolyma Oct 02 '12 at 22:26
  • @singpolyma Works for me on Chrome. May be browser-specific. –  Oct 02 '12 at 22:40
  • @duskwuff weird. I just tested in the latest Chromium for my plaform and that also did not work. – singpolyma Oct 02 '12 at 22:47

1 Answers1

1

You can do this by manually firing/dispatching an event (depending on the browser, fireEvent/dispatchEvent) directly on the DOM element. Code from this answer will handle the event dispatching, you'll just need to execute it against a DOM element and not the jQuery wrapper.

Community
  • 1
  • 1
Oleg
  • 24,465
  • 8
  • 61
  • 91
  • Accepting this because it seems to be the only way. jQuery just doesn't support this, as far as I can tell :) – singpolyma Oct 18 '12 at 16:32