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

141
Views
prevent keyup when input focus

I intent to trigger something with spacebar key, but spacebar should be prevent trigger when input is focus.

I'm trying with this way, but it doesnt work well is there any suggestion how to do it right.

$(document).ready(function(){
  $('input').on('blur', function(){
    $(document).on('keyup', function(e){
      if(e.keyCode == 32){
        alert("pressed!");
      }
    });
  }); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label> Name</label>
  <input type="text" />
  <br />
  <label>Pass</label>
  <input type="text" />
</form>

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Detach the handler with .off() while input has focus:

$(function(){
    $(document).on('keyup', function(e) {
        if(e.keyCode == 32){
            alert("pressed!");
        }
    });
    $('input').on('focus', function() {
        $(document).off('keyup');
    });
    $('input').on('blur', function() {
        $(document).on('keyup', function(e) {
            if(e.keyCode == 32){
                alert("pressed!");
            }
        });
    });
   
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <label> Name</label>
  <input type="text" />
  <br />
  <label>Pass</label>
  <input type="text" />
</form>

about 4 years ago · Santiago Trujillo 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!