Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
Can't remove keydown event listener programtically

The CTRL + e combination on is quite annoying, I'm need to remove the keydown handler.

From chrome the event listener is on textarea:

enter image description here

So I created a script and tested it in devtools:

Array.from(document.querySelectorAll("textarea")).forEach(e => {
    const ev = getEventListeners(e)
    if (ev['keydown']) {
        console.log(ev['keydown'])
        ev['keydown'].forEach((row) => {
            e.removeEventListener('keydown', row.listener, false)
        })
    }
})

But the event listener is still there, what was wrong?

EDIT

As @wahoowa suggested, this worked partially, CTRL + E no longer creates the backtick on github.

But it doesn't do the default action on Mac as well. The default action is to jump to the end of line, how can I do the default behavior when CTRL + E is pressed?

document.querySelectorAll("textarea").forEach(e => {
    e.addEventListener("keydown", ev => {
        if (ev.key == 'e' && ev.ctrlKey) {
            ev.preventDefault();
            ev.stopPropagation();
        }
    })
})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Instead of removing it, you could add an event listener to all textarea elements to prevent the default behavior and also stop propagation of the event to parent elements.

document.querySelectorAll("textarea").forEach(e => {
    e.addEventListener("keydown", ev => {
        ev.preventDefault();
        ev.stopPropagation();
    })
})

Note that this is solution to your scenario, but it does not answer your initial question of how to remove an event listener. If there were other keydown event listeners added to the textarea elements, those would not be removed. In order to actually remove the event listeners, you can look at these other questions:

  • Remove All Event Listeners of Specific Type
  • Javascript/DOM: How to remove all event listeners of a DOM object?
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda