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 currently have a label that is absolute positioned within a textarea. I'm trying
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I have a textarea tag that has text I want a user to edit.
I have this markup:- <textarea id=TextBox1></textarea> <span id=validator></span> And the following jQuery:- jQuery('#TextBox1').live('input', function()
I currently have <textarea id=Commentary rows=5 cols=40 runat=server></textarea> Then in the event insertCommand.Parameters[@Commentary].Value =
Currently have a drop down menu that is activated on a hover (from display:none
I currently have one project that currently contains multiple packages. These packages make up
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
Currently I have a form class Project extends AbstractType { public function buildForm(FormBuilder $builder,
I have the following function to get the value in a textarea with the

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.