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

  • SEARCH
  • Home
  • 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 403859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:15:19+00:00 2026-05-12T17:15:19+00:00

Currently I have a textarea with an onkeypress event specified in the HTML tag.

  • 0

Currently I have a textarea with an onkeypress event specified in the HTML tag. I am trying to capture this keypress and save it as a function that will be used to override it, as I want to add another condition to when it is run. Here is the code I am trying to implement.

$(document).ready(function(e) {
    var defaultKeypress = $('textarea').keypress;
    $('textarea').keypress(function(e) {
        if (fieldsChanged) {
            fieldsChanged = false;
        }
        else {
            defaultKeypress(e);
        }

    });
});

The issue I am having is with IE 7 & 8. Both fire the original keypress (even if “defaultKeypress(e);” is commented out) and the new keypress are firing. (Original first, then new). Other than removing the onkeypress attribute from the textarea tag (I can’t do this because if how the code is generated), is there a way to disable that original keypress from firing?

Thanks!
Nutzy

  • 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-05-12T17:15:19+00:00Added an answer on May 12, 2026 at 5:15 pm

    jQuery’s keypress method adds the handler you pass to it and does not override anything. This is very much by design. This way you can attach multiple event handlers to the element in question and not have to worry about clobbering any existing handlers.

    You actually want to clobber the existing handler. You can’t use unbind for this. You can just remove the handler yourself anyway though:

    var $el = $('textarea'),
        defaultKeypress = $el.attr('onkeypress');
    
    $el.removeAttr('onkeypress');
    
    $el.keypress(function(e) {
        if (fieldsChanged) {
            fieldsChanged = false;
        }
        else {
            return defaultKeypress.apply(this, arguments);
        }
    });
    

    Notice I call defaultKeypress with the correct scope using apply, as well as using return to pass its result back. This is all to maintain the standard event handler environment the browser would have called it with.

    References:

    • jQuery’s removeAttr
    • The apply function
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textarea tag that has text I want a user to edit.
I currently have an MS Access application that connects to a PostgreSQL database via
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a class and I'm trying to create an easy GUI to
I currently have a DetailsView in ASP.NET that gets data from the database based
I have a textarea in a html form where I put: line1 line2 line3
I have a page with radio buttons and a textarea that populates data dynamically
Hey all, I have an HTML form on my website which currently only uses
I have a textarea form in my html. If the user hits enter between
So on our site we currently have a textarea for commenting on certain items.

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.