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

177
Visualizações
How to make this so the visibility will not change if the event is triggered from a typebox target?

Say I have a button, like this:

    let btn = document.createElement("button");
    btn.style = "position: fixed; top: 0; left: 0; visibility: visible;";
    btn.innerText = 'Click me!';
    document.body.appendChild(btn);
    btn.onclick = function() {
        btn.innerText = 'Clicked';
    };
    
    document.addEventListener("keydown", function(event) {
        if (event.code === 'KeyU') {
            btn.style.visibility = btn.style.visibility == "hidden" ? "visible" : "hidden";
        }
    });

How could I make it so that the visibility will only change when you're not typing into some sort of textbox or searchbar?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As @Bravo mentioned in the comments of this question, you can read into event.target in order to detect what element was in focus when the user pressed the key.
event.target contains a reference to the element that was in focus. https://developer.mozilla.org/en-US/docs/Web/API/Event/target. Here's an example where it will only change the visibility if the key wasn't being pressed in a textarea.

    let btn = document.createElement("button");
    btn.style = "position: fixed; float: left; top: 0; left: 0; visibility: visible;";
    btn.innerText = 'Click me!';
    document.body.appendChild(btn);
    btn.onclick = function() {
        btn.innerText = 'Clicked';
    };
    
    document.addEventListener("keydown", function(event) {
        if (event.code === 'KeyK' && event.target.tagName !== 'textarea') {
            btn.style.visibility = btn.style.visibility == "hidden" ? "visible" : "hidden";
        }
    });
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