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

190
Views
JS ev.preventDefault() stops working after switching to another window

In the following function I try to block the scrolling by keys and it works fine, but if I switch to another window and switch back to the browser window, the ev.preventDefault() function doesn't work anymore.

  • The key_next() and key_last() functions are still triggered
  • I'm using Windows 10 and chrome
    function start(){
        window.addEventListener("keyup",(ev)=>{

            if(ev.code === "ArrowUp"){
                ev.preventDefault();
                key_last();

            }
            else if(ev.code === "ArrowDown"){
                ev.preventDefault();
                key_next();
 
            }
            else if(ev.code === "ArrowLeft"){
                ev.preventDefault();
                key_last();

            }
            else if(ev.code === "ArrowRight"){
                ev.preventDefault();
                key_next();
            }
            else if(ev.code === "Space"){
                ev.preventDefault();
            }
        });
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I added this into my start() function, now it works even if I switch windows.

window.addEventListener("keydown",(ev)=>{
        if(ev.code === "ArrowUp" || ev.code === "ArrowDown" || ev.code === "ArrowLeft" || ev.code === "ArrowRight" || ev.code === "Space"){
            ev.preventDefault();
        }
    });

    window.addEventListener("keypress",(ev)=>{
        if(ev.code === "ArrowUp" || ev.code === "ArrowDown" || ev.code === "ArrowLeft" || ev.code === "ArrowRight" || ev.code === "Space"){
            ev.preventDefault();
        }
    });
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!