Can I fire a jQuery "change" event on an arbitrary type of HTML element?

Say I have a jQuery object, el , that has selected an element. Is it legal, safe, and reasonable to call el.trigger("change") if the selected item is a DIV? What about other types of elements? In this case, can I call el.change() ?

The jQuery documentation for .change () says:

A change event is dispatched to an element when its value changes. This event is limited to <input>, <textarea> and <select> elements.

I don’t understand what “limited” means here. This may be due to the fact that these are only three types of elements that will automatically create these events, but may mean that other elements are not allowed.

Empirically, Chrome v28 seems to allow this, but I want to know if I can expect it to work in general.

goal

I have a pseudo-control consisting of a set of buttons and scrolls wrapped in a div. Each instance of the control supports and controls the value, which is changed by pressing the control buttons. When the value changes, I need to send an event from the div so that the rest of the page can respond. I don’t want to listen to click events outside the control, as this associates the surrounding code with the internal controls, and not all clicks change the value.

I could create a new event name, but the built-in "change" event seems conceptually correct, so I would prefer to use it if possible. As an added bonus, my page already has a “change” handler associated the right place with the correct behavior (because I have input and selection elements on the page).

I need to support IE8 and higher, in case the answer depends on the browser version and version.

+4
source share
1 answer

There are no restrictions, you can trigger any type of event that you like on any HTML element.
The jQuery documentation just tells you that change only runs automatically on <input> , <textarea> and <select>

+2
source

Source: https://habr.com/ru/post/1492322/


All Articles