0

i have a function , in it i want to fire an event whenever the input value changes.

            var input = me.lengthSelector; //'lengthSelector': '.fv--table-length input'

            // Create a new 'change' event
            let event = new Event('change');
            // Dispatch it.
            $(input).dispatchEvent(event);

so whenever the event "change" is fired, a function onChange() would be executed that is defined somehwere else and functions correctly. but why does it say dispathcEvent() is not a function in the console ? i tried using trigger() but is says the same thing :(

Zyfella
  • 60
  • 6
  • 3
    Because jQuery does not have a `dispatchEvent()` method. It is [a native element method](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent). jQuery has [`.trigger()`](https://api.jquery.com/trigger/). See: [How to trigger event in JavaScript?](https://stackoverflow.com/q/2490825) | [How do I programmatically trigger an “input” event without jQuery?](https://stackoverflow.com/q/35659430) | [Dispatch event with data](https://stackoverflow.com/q/23725816) – VLAZ Nov 23 '21 at 12:10
  • Thanks now it works using `$(input).trigger("change");`. I was mistakenly using trigger(event) instead of a string inside the trigger() – Zyfella Nov 23 '21 at 12:27

1 Answers1

2

its just input.dispatchEvent(event). this is not a jquery function

Flex Elektro Deimling
  • 1,599
  • 1
  • 17
  • 16