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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:42:02+00:00 2026-05-26T03:42:02+00:00

I need to have a number that’s displayed on a web page that will

  • 0

I need to have a number that’s displayed on a web page that will increase or decrease when the up or down arrow keys are pressed. I found/massaged together javascript file to do this but I can’t seem to get it to work with my HTML. I was trying to link it to a html textbox but it would not work.

if someone could help me with the HTML to get this working that would be great.

var setTimeoutId; 
var keyIs = "up"; 

 function myIncrementFunction()
{
        var num = parseFloat(myText.value)+1;
        myText.value = num; 

}

myText.onkeydown = function(e)
{
keyIs = "down";

if(keyIs == "down")
    {
        var e = e || event ;
        if (e.keyCode == 38)
            {    
                for(var s=0; s<1; s++)
                    setTimeoutId = setTimeout('myIncrementFunction()',100); 
            }
    }
}

myText.onkeyup = function(e)
{ 
 keyIs = "up"; 
}

Tried this and it still is not working.. > ?

number.html

<html>
<head>
<script type="text/javascript" src="number.js"></script>
</head>

<body>

<input type="text" id="myText" />

</body>

</html>

number.js

var myText = document.getElementById("myText");

// Capture keyDown events
myText.onkeydown = function(e) {
// "38" is the up arrow key
if (e.keyCode == 38) {
    // increment the value in the text input
    myText.value++;
// "40" is the down arrow key
} else if (e.keyCode == 40) {
    // decrement the value in the text input
    myText.value--;
}
}

I don’t understand why it works in the example posted and when I save my files and open it in the browser it will not work!

I’m on OSX Lion using Chrome/Safari

  • 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-26T03:42:03+00:00Added an answer on May 26, 2026 at 3:42 am

    It looks like there are a couple of things going on. First, as jayp points out in his comment, you aren’t defining what myText is.

    Secondly, I think you’re over-complicating this a bit. How about trying something like this:

    Give your text input an ID, something like <input type="text" id="myText" />

    Then use something like this:

    // Assign our text input to a variable
    var myText = document.getElementById("myText");
    
    // Capture keyDown events
    myText.onkeydown = function(e) {
        // "38" is the up arrow key
        if (e.keyCode == 38) {
            // increment the value in the text input
            myText.value++;
        // "40" is the down arrow key
        } else if (e.keyCode == 40) {
            // decrement the value in the text input
            myText.value--;
        }
    }
    

    This is a pretty simplified example, but should get you pointed in the right direction. Hope it helps.

    See a working example at JSFiddle

    Edit:

    It’s not working in your case because the script is trying to find the input element before the page is fully loaded. You can move your script to the bottom of the page like this:

    <html>
        <head></head>
        <body>
            <input type="text" id="myText" />
            <script type="text/javascript" src="number.js"></script>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of reports that need to be both run separately and
I have a number of traceroutes that i need to compare against each other
I have a number of reports that I run against my database that need
I have a number of events that circulate throughout a year. I need to
I have a number rather large, complex xml documents that I need to loop
Here's what I need to fetch: - posts that have comments - number of
I have HTML that I need to extract a part number from, the HTML
I have a lotus view that stores a number. I need to perform some
I currently have a SQL query that returns a number of fields. I need
I have a number of departments that need to put forms online...I'm wondering if

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.