How to trigger event in JavaScript?

前端 未结 18 1855
情深已故
情深已故 2020-11-21 04:48

I have attached an event to a text box using addEventListener. It works fine. My problem arose when I wanted to trigger the event programmatically from another

相关标签:
18条回答
  • 2020-11-21 05:10

    The most efficient way is to call the very same function that has been registered with addEventListener directly.

    You can also trigger a fake event with CustomEvent and co.

    Finally some elements such as <input type="file"> support a .click() method.

    0 讨论(0)
  • 2020-11-21 05:11

    You can use fireEvent on IE 8 or lower, and W3C's dispatchEvent on most other browsers. To create the event you want to fire, you can use either createEvent or createEventObject depending on the browser.

    Here is a self-explanatory piece of code (from prototype) that fires an event dataavailable on an element:

    var event; // The custom event that will be created
    if(document.createEvent){
        event = document.createEvent("HTMLEvents");
        event.initEvent("dataavailable", true, true);
        event.eventName = "dataavailable";
        element.dispatchEvent(event);
    } else {
        event = document.createEventObject();
        event.eventName = "dataavailable";
        event.eventType = "dataavailable";
        element.fireEvent("on" + event.eventType, event);
    }
    
    0 讨论(0)
  • 2020-11-21 05:13

    I just used the following (seems to be much simpler):

    element.blur();
    element.focus();
    

    In this case the event is triggered only if value was really changed just as you would trigger it by normal focus locus lost performed by user.

    0 讨论(0)
  • 2020-11-21 05:14

    Modified @Dorian's answer to work with IE:

    document.addEventListener("my_event", function(e) {
      console.log(e.detail);
    });
    
    var detail = 'Event fired';
    
    try {
    
        // For modern browsers except IE:
        var event = new CustomEvent('my_event', {detail:detail});
    
    } catch(err) {
    
      // If IE 11 (or 10 or 9...?) do it this way:
    
        // Create the event.
        var event = document.createEvent('Event');
        // Define that the event name is 'build'.
        event.initEvent('my_event', true, true);
        event.detail = detail;
    
    }
    
    // Dispatch/Trigger/Fire the event
    document.dispatchEvent(event);
    

    FIDDLE: https://jsfiddle.net/z6zom9d0/1/

    SEE ALSO:
    https://caniuse.com/#feat=customevent

    0 讨论(0)
  • 2020-11-21 05:16

    You can use below code to fire event using Element method:

    if (!Element.prototype.triggerEvent) {
        Element.prototype.triggerEvent = function (eventName) {
            var event;
    
            if (document.createEvent) {
                event = document.createEvent("HTMLEvents");
                event.initEvent(eventName, true, true);
            } else {
                event = document.createEventObject();
                event.eventType = eventName;
            }
    
            event.eventName = eventName;
    
            if (document.createEvent) {
                this.dispatchEvent(event);
            } else {
                this.fireEvent("on" + event.eventType, event);
            }
        };
    }
    

    if (!Element.prototype.triggerEvent) {
        Element.prototype.triggerEvent = function (eventName) {
            var event;
    
            if (document.createEvent) {
                event = document.createEvent("HTMLEvents");
                event.initEvent(eventName, true, true);
            } else {
                event = document.createEventObject();
                event.eventType = eventName;
            }
    
            event.eventName = eventName;
    
            if (document.createEvent) {
                this.dispatchEvent(event);
            } else {
                this.fireEvent("on" + event.eventType, event);
            }
        };
    }
    
    var input = document.getElementById("my_input");
    var button = document.getElementById("my_button");
    
    input.addEventListener('change', function (e) {
        alert('change event fired');
    });
    button.addEventListener('click', function (e) {
        input.value = "Bye World";
        input.triggerEvent("change");
    });
    <input id="my_input" type="input" value="Hellow World">
    <button id="my_button">Change Input</button>

    0 讨论(0)
  • 2020-11-21 05:18

    What you want is something like this:

    document.getElementByClassName("example").click();
    

    Using jQuery, it would be something like this:

    $(".example").trigger("click");
    
    0 讨论(0)
提交回复
热议问题