Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!