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.

6
  • 1
    @singpolyma What do you mean by native browser event binding? .addEventListener()? I am able to invoke such a handler: 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: jsfiddle.net/mnrUt/1 Commented Oct 2, 2012 at 21:12
  • @adeneo See my two demos above. Commented Oct 2, 2012 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 ! Commented Oct 2, 2012 at 21:42
  • @ŠimeVidas interesting that it works for click. But not for change on a select box: jsfiddle.net/qxpXV/2 Commented Oct 2, 2012 at 22:26
  • @singpolyma Works for me on Chrome. May be browser-specific. Commented Oct 2, 2012 at 22:40

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Accepting this because it seems to be the only way. jQuery just doesn't support this, as far as I can tell :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.