Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:42:15+00:00 2026-06-01T04:42:15+00:00

How to do that? I tried: var key = event.which || event.keyCode || event.charCode;

  • 0

How to do that?

I tried:

var key = event.which || event.keyCode || event.charCode;

if(key == 8) alert('backspace');

but it doesn’t work…

If I do the same on the keypress event it works, but I don’t want to use keypress because it outputs the typed character in my input field. I need to be able to control that


my code:

  $('#content').bind('input', function(event){

    var text = $(this).val(),
        key = event.which || event.keyCode || event.charCode;

    if(key == 8){
      // here I want to ignore backspace and del
    }

    // here I'm doing my stuff
    var new_text = 'bla bla'+text;
    $(this).val(new_text);
  });

no character should be appended in my input, besides what I’m adding with val()
actually the input from the user should be completely ignored, only the key pressing action is important to me

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:42:17+00:00Added an answer on June 1, 2026 at 4:42 am

    Use .onkeydown and cancel the removing with return false;. Like this:

    var input = document.getElementById('myInput');
    
    input.onkeydown = function() {
        var key = event.keyCode || event.charCode;
    
        if( key == 8 || key == 46 )
            return false;
    };
    

    Or with jQuery, because you added a jQuery tag to your question:

    jQuery(function($) {
      var input = $('#myInput');
      input.on('keydown', function() {
        var key = event.keyCode || event.charCode;
    
        if( key == 8 || key == 46 )
            return false;
      });
    });
    

    ​

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code that I tried is as follows: var dataO = new Object(); dataO.numberId
Having an issue here that I have tried everything I can think of but
tried to research on that but sometimes I seem to lack some googling skills...
I tried to write a program that uses threads, but couldn't understand the o/p.
My understanding of the Integer.TryParse() function was that it tried to parse an integer
I've noticed yesterday by looking into my apache error log that someone tried to
I tried to do that, and I found this errors: >>> import re >>>
I tried making a wrapper class that encapsulates an object, a string (for naming
I tried to write a function that calculates a hamming distance between two codewords
I have tried many things to get this working correctly but I can't Here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.