• ARعربي
  • ENEnglish
  • ESEspañol
  • FAفارسی
  • FRFrançais
  • IDIndonesia
  • ITItaliano
  • JA日本語
  • KO한국어
  • RUРусский
  • TRTürkçe
  • UKУкраїнська
  • ZH简体中文

Nous souhaitons rendre ce projet open source disponible pour les gens du monde entier.

Aidez-nous à traduire le contenu de ce tutoriel dans votre langue!

    AcheterEPUB/PDF
    Carte du tutoriel
    Partager
    • Tutoriel
    • Navigateur : Document, Évènements, Interfaces
    • Introduction to Events
    • Introduction to browser events
    retour au cours
    Ce contenu n'est disponible que dans les langues suivantes :English, Español, فارسی, Indonesia, Italiano, 日本語, 한국어, Русский, Українська, 简体中文. Merci de

    Which handlers run?

    importance: 5

    There’s a button in the variable. There are no handlers on it.

    Which handlers run on click after the following code? Which alerts show up?

    button.addEventListener("click", () => alert("1"));
    
    button.removeEventListener("click", () => alert("1"));
    
    button.onclick = () => alert(2);

    The answer: 1 and 2.

    The first handler triggers, because it’s not removed by removeEventListener. To remove the handler we need to pass exactly the function that was assigned. And in the code a new function is passed, that looks the same, but is still another function.

    To remove a function object, we need to store a reference to it, like this:

    function handler() {
      alert(1);
    }
    
    button.addEventListener("click", handler);
    button.removeEventListener("click", handler);

    The handler button.onclick works independently and in addition to addEventListener.

    • © 2007—2025  Ilya Kantor
    • à propos du projet
    • nous contacter