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

116
Views
Toggle keypress event everytime condition changes

The following is my html code:

<input type="text" class="form-control" id="emp" onkeypress="return (event.charCode > 64 && 
                event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)" >

I have a radiobutton which when checked should remove the onkeypress event and when unchecked should activate it. This is my js:

input.addEventListener('change', function() {
    if (input.checked) {
      document.getElementById("tag").innerHTML="No";
      document.getElementById("emp").value="";
      document.getElementById("emp").removeAttribute("onkeypress");
    } else {
      document.getElementById("tag").innerHTML="Name";
      document.getElementById("emp").value="";
      document.getElementById("emp").setAttribute("onkeypress", "");
    }
});

But when I toggle the radiobutton, the code doesn't work properly. Can you please help me?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think it's better to handle this feature in your onkeypress function, just check if the radio button is checked do anything you want, if not return null

about 4 years ago · Juan Pablo Isaza Report

0

You are setting onkeypress attribute to an empty string in the unchecked case, so it will do nothing, same as if the attribute is removed. You'd need to set it back to the original text, which you could get with getAttribute and store.

var onkeypress_code = document.getElementById("emp").getAttribute("onkeypress");
input.addEventListener('change', function() {
    if (input.checked) {
      document.getElementById("tag").innerHTML="No";
      document.getElementById("emp").value="";
      document.getElementById("emp").removeAttribute("onkeypress");
    } else {
      document.getElementById("tag").innerHTML="Name";
      document.getElementById("emp").value="";
      document.getElementById("emp").setAttribute("onkeypress", onkeypress_code);
    }
});

But I would recommend doing this differently. You can use addEventListener and removeEventListener (removing the onkeypress from the HTML):

function emp_onkeypress(event) {
    return (
        (event.charCode > 64 && event.charCode < 91) ||
        (event.charCode > 96 && event.charCode < 123)
    );
}
function checkbox_onchange(event) {
    if (input.checked) {
      document.getElementById("tag").innerHTML="No";
      document.getElementById("emp").value="";
      document.getElementById("emp").removeEventListener("keypress", emp_onkeypress);
    } else {
      document.getElementById("tag").innerHTML="Name";
      document.getElementById("emp").value="";
      document.getElementById("emp").addEventListener("keypress", emp_onkeypress);
    }
}
input.addEventListener('change', checkbox_onchange);
checkbox_onchange(); // update based on initial checkbox state

Or you can simply handle the checked/unchecked state in your keypress handler. If you define the keypress handler in your JS, you will have access to the input variable.

function emp_onkeypress(event) {
    if (input.checked) {
        return;
    }
    return (
        (event.charCode > 64 && event.charCode < 91) ||
        (event.charCode > 96 && event.charCode < 123)
    );
}
document.getElementById("emp").addEventListener("keypress", emp_onkeypress);

input.addEventListener('change', function() {
    if (input.checked) {
      document.getElementById("tag").innerHTML="No";
      document.getElementById("emp").value="";
    } else {
      document.getElementById("tag").innerHTML="Name";
      document.getElementById("emp").value="";
    }
});

Also, charCode is deprecated; key and code are the recommended APIs.

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!