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 8386627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:59:36+00:00 2026-06-09T17:59:36+00:00

I’m trying to make it so I don’t have a write a function for

  • 0

I’m trying to make it so I don’t have a write a function for every input field I want to use this on. Rather sending the element id to the function and only having one function that I can recycle.

Works like this

    <input name="field" id="field" type="text" onKeyPress="onlyNum()" />

    <script type="text/javascript"> 
        function onlyNum() {
            var name = $("#field");
            name.keypress(function (e) {
                if (e.which > 0 && // check that key code exists
                    e.which != 8 && // allow backspace
                    !(e.which >= 48 && e.which <= 57) // allow 0-9
                    ) {
                    e.preventDefault();
                }
            });
        }
    </script>

But it doesn’t work like this, however this is what I’m going for:

    <input name="field" id="field" type="text" onKeyPress="onlyNum('field')" />

    <script type="text/javascript"> 
        function onlyNum(theInput) {
            var name = document.getElementById(theInput);
            name.keypress(function (e) {
                if (e.which > 0 && // check that key code exists
                    e.which != 8 && // allow backspace
                    !(e.which >= 48 && e.which <= 57) // allow 0-9
                    ) {
                    e.preventDefault();
                }
            });
        }
    </script>

So does anyone know what’s wrong with the second example? And how can I get that to work. Thanks

  • 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-09T17:59:38+00:00Added an answer on June 9, 2026 at 5:59 pm

    The problem with your second example is that you are trying to call .keypress on a DOM element. .keypress is a method of jQuery objects though.

    The whole approach is strange though. What are you trying to do is binding a new event handler to the element upon each key press. That is, after three keys have been pressed, you have assigned three event handlers to the same element which are all doing the same.

    What you should be doing is assigning a class to each element that you want to bind the event handler to, select them with jQuery and bind the handler once.

    For example:

    <input name="field" id="field" type="text" class="someClass"/>
    

    Binding the handler:

    // will bind the event handler to each 'input' field with class 'someClass'
    $('input.someClass').keypress(function (e) {
        if (e.which > 0 && // check that key code exists
            e.which != 8 && // allow backspace
            !(e.which >= 48 && e.which <= 57) // allow 0-9
        ) {
            e.preventDefault();
        }
    });
    

    If you cannot modify the HTML, then make use of the multiple selector and list the IDs:

    // adds the event handler to the elements with the IDs 'field', 'someID' and
    // 'someOtherID'
    $('#field, #someID, #someOtherID').keypress(function() {
        //...
    });
    

    That’s how jQuery works. You might want to read some of the tutorials to get a better idea of it.


    Here is the less recommendable way to fix your code:

    I already said that you are binding a handler to the keypress event inside the keypress event handler, which does not make sense. You already assigned the event handler via the onkeypress HTML attribute. You don’t need the ID of the element. All you have to do is pass the event object to your function.

    Example:

    <input name="field" id="field" type="text" onKeyPress="onlyNum(event)" />
    

    JavaScript:

    function onlyNum(e) {
        if (e.which > 0 && // check that key code exists
            e.which != 8 && // allow backspace
            !(e.which >= 48 && e.which <= 57) // allow 0-9
        ) {
            e.preventDefault();
        }
    }
    

    The biggest difference with this approach is that event is a native event object, not a jQuery event object. That also means that calling e.preventDefault() will fail in IE8 and below since this method does not exist. Also you might have to use e.keyCode instead of e.which. jQuery takes care of all these differences.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I want to construct a data frame in an Rcpp function, but when I

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.