简体   繁体   中英

How to trigger a change event with a custom HTMLElement as the target?

I've an eventlistener registred on a section HTML tag which embedded various dropdowns:

function dropdownChange(e) {
    window.console.log("The event is detected");
    if (e.target.tagName === "SELECT")
        window.console.log("The event's target is a dropdown"); // Some jobs
    else
        window.console.log("The event's target is NOT a dropdown"); // Some jobs
}

document.getElementById("filterSection").addEventListener("change", dropdownChange);

It works fine.

That I want to do is to trigger once a change event when my page is loaded. As far as I understand the Mozilla documentation I should perform when my DOM is ready the following action:

document.getElementById("periode").dispatchEvent(new Event("change"));

But the event is not trigger (I get no trace into my console). I thought that the event propagation would make the job...

If I execute:

document.getElementById("filterSection").dispatchEvent(new Event("change"));

The event is well triggered but the target is not the good one.

What am I doing wrong?

Dropdowns' HTML code:

<section id="filterSection">
    <label>Période</label>
    <select name="periode" id="periode">
        <option value="2014-08">2014 Août</option>
        <option value="2014-07">2014 Juillet</option>
        <option value="2014-06">2014 Juin</option>
        <option value="2014-05">2014 Mai</option>
        <option value="2014-04">2014 Avril</option>
        <option value="2014-03">2014 Mars</option>
        <option value="2014-02">2014 Février</option>
        <option value="2014-01">2014 Janvier</option>
        <option value="2013-12">2013 Décembre</option>
        <option value="2013-11">2013 Novembre</option>
        <option value="2013-10">2013 Octobre</option>
        <option value="2013-09">2013 Septembre</option>
        <option value="2013-08">2013 Août</option>
        <option value="2013-07">2013 Juillet</option>
        <option value="2013-06">2013 Juin</option>
    </select>
    <!-- Other dropdowns...-->
</section>

My main browser target is Firefox.17

I would suggest using document.querySelector instead of getById, since it is more reliable.

So change adding/dispatching events to this below.

document.querySelector("#periode").addEventListener("change", dropdownChange);

document.querySelector("#periode").dispatchEvent(new Event("change"));

UPDATE: To have a parent, and let all children have the same event , following code:

function dropdownChange(e) {
    window.console.log("The event is detected");
    if (e.target.tagName === "SELECT") window.console.log("The event's target is a dropdown");
    else window.console.log("The event's target is NOT a dropdown");
}
//shiv, add a forEach to nodeList prototype to use forEach method on querySelectorAll lsit.
NodeList.prototype.forEach = Array.prototype.forEach; 

var parent = document.querySelectorAll("#filter select");
parent.forEach(function(e){
    e.addEventListener("change", dropdownChange);
});
document.querySelector("#filter select").dispatchEvent(new Event("change"));

http://jsfiddle.net/Holybreath/5sda7zsc/6/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM