Posted 3 months ago. Visible to the public. Repeats.

Events triggered by jQuery cannot be observed by native event listeners

jQuery has a function $.fn.trigger(). You can use it to dispatch an event on a jQuery object:

let $element = $('.foo') $element.trigger('change')

A caveat is that such an event will be received by jQuery event listeners, but not by native event listeners:

let $element = $('.foo') $element.on('change', event => console.log('I will be called')) $element[0].addEventListener('change', event => console.log('I WON'T be called')) $element.trigger('change')

This is currently a WONTFIX for jQuery (see closed issues #2476, #3347).

Note that if you trigger a native event instead, it will be received both by jQuery event listeners ($.fn.on()) and native event listeners (Element#addEventListeners()):

let $element = $('.foo') $element.on('change', event => console.log('I will be called')) $element[0].addEventListener('change', event => console.log('I will also be called')) let event = new CustomEvent('change') $element[0].dispatchEvent(event)

Impact and workarounds

This is an issue if and only if:

  1. Your app has native event listeners registered using Element#addEventListener()
  2. Your app triggers event listeners using jQuery's trigger()

If possible, change your app so it doesn't use jQuery's trigger() function. Instead trigger native events using Element#dispatchEvent() or Unpoly's up.emit().

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Henning Koch
Last edit:
2 months ago
by Dominik Schöler
select2, up-observe
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more