How do I fire dispatchEvent?

  • 0
    Hello
    there is someone else's site, it has imputs, these impots are used to handle the onchange event,
    which checks if the input has occurred using the keyboard.
    Kind of like via react, earlier element.dispatchEvent (new Event ('change', {bubbles: true}));
    firel the change event and inputs passed the entered values ​​through js, now
    dispatchEvent does not fire anything in any way, but only changes the value in the input, which is only visually changed and a completely different value is sent to the server, what was entered from the keyboard ...
    How can change be catalyzed now?
    JavaScript Anonymous, Nov 7, 2020

  • 3 Answers
  • 0
    if anyone needs it, I found a solution outside of ReactTestUtils.Simulate



    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://unpkg.com/react-trigger-change/dist/react-trigger-change.js';
    document.head.appendChild(script);
    var cap = document.querySelectorAll('input');
    cap[1].value = 8.0000;
    reactTriggerChange(cap[1]);
    cap[1].dispatchEvent(new Event('blur', { bubbles: true }));
    Lydia Hansen

  • 0
    Well, you either missed an element, or you are sending the wrong event. Personally, I do not subscribe to onChange inputs, but I subscribe to onInput, maybe you should send it exactly new Event ("input")



    React or not react does not matter, since there will also be a subscription to events in the dom, and they can be sent from the dom.



    But if react or vue, or analogs, then the link to the element may go bad after updating the component. This is what happens when you type. Perhaps you need to fetch an element from the dom every time after the event is dispatched.
    Anonymous

  • 0
    Here is a simpler solution without unnecessary scripts:

    https://stackoverflow.com/a/46012210/14550686
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!