Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
eventListener gets assigned correctly but does not trigger the function

I have a class called ContextMenu and it is made to replace the default contextmenu that the browser shows whenever you right click on the page.

This menu needs some eventListeners applied to it's children.

        <div id="contextmenu" class="hidden">
            <ul>
                <li id="download-file" class="action">Download</li>
                <li id="rename-file" class="action">Rename</li>
                <li id="delete-file" class="action">Delete</li>
                <li id="details-file" class="action">Details</li>
            </ul>
        </div>

this is the html for the contextmenu

export default class ContextMenu
{
    contextMenu
    events
    actions
    selectedItem

    constructor(HTMLContextMenu)
    {
        this.contextMenu = HTMLContextMenu

        window.document.onmousedown = (event) => this.show(event)

        this.events = ['click', 'touchstart']
        this.actions = window.document.querySelectorAll('.action') // all elements that have to trigger an eventListener have the class "action"

        for (const event in this.events)
        {
            this.actions[0].addEventListener(event, this.download, false)
            this.actions[1].addEventListener(event, this.rename, false)
            this.actions[2].addEventListener(event, this.delete, false)
            this.actions[3].addEventListener(event, this.details, false)
        }

        this.selectedItem = null
    }

this is the javascript, i did not paste the entire class, just what's relevant for the problem. As you can see the for in loop takes care of the eventListeners applying 8 listeners in total. The problem is that whenever i click the element nothing happens

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I have found the issue. The for (const event in this.events) loop is wrong, it has to be for (const event of this.events) in order to make it work.

    for (const event of this.events)
    {
        this.actions[0].addEventListener(event, this.download, false)
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

As iQucik said you are using the wrong keyword in your loop. In case you're using iterable objects you have to use of keyword otherwise use in keyword for enumerable objects.

Some references:

MDN Docs for-of loop: MDN Docs for..of

MDN Docs for-in loop: MDN Docs for..in

export default class ContextMenu
{
    contextMenu
    events
    actions
    selectedItem

    constructor(HTMLContextMenu)
    {
        this.contextMenu = HTMLContextMenu

        window.document.onmousedown = (event) => this.show(event)

        this.events = ['click', 'touchstart']
        this.actions = window.document.querySelectorAll('.action') // all elements that have to trigger an eventListener have the class "action"

        for (const event of this.events)
        {
            this.actions[0].addEventListener(event, this.download, false)
            this.actions[1].addEventListener(event, this.rename, false)
            this.actions[2].addEventListener(event, this.delete, false)
            this.actions[3].addEventListener(event, this.details, false)
        }

        this.selectedItem = null
    }
        <div id="contextmenu" class="hidden">
            <ul>
                <li id="download-file" class="action">Download</li>
                <li id="rename-file" class="action">Rename</li>
                <li id="delete-file" class="action">Delete</li>
                <li id="details-file" class="action">Details</li>
            </ul>
        </div>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda