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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:30:49+00:00 2026-06-02T21:30:49+00:00

I have a basic HTML form named Calculator, with a button named "btnOne". What

  • 0

I have a basic HTML form named Calculator, with a button named "btnOne". What I want is for when the user presses the 1 key on the keyboard, the "btnOne" HTML button’s onClick event to occur.

To attempt to accomplish this, I placed this function in the Head tag of my document:

<head>
     <SCRIPT LANGUAGE="JavaScript"> 
             
         //other functions...

        document.onkeydown = function(e){
            if (!e) e = window.event;
            if(e.keycode == '49') {
                document.Calculator.btnOne.click();
            }
            return false;
        }
      </SCRIPT>
</head>

Am I just putting this snippet in the wrong spot? Is there something else I need to do to make the function run? I’m really a beginner with web development and don’t know what I’m doing! Of course, once I get figured out how to get this button to work, I will add if statements for all of the other buttons.

  • 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-02T21:30:51+00:00Added an answer on June 2, 2026 at 9:30 pm

    check out http://api.jquery.com/category/events/ for a much easier and cleaner way to write this. What you have is basically right, though you’d do well to wrap this in a document ready event, so that the events bind after the dom has loaded.

    with jQuery your code would look something like this

    jQuery(function() {
        jQuery(window).keypress(function(e){
             if (e.which === 49) {
                   jQuery("#btnOne").click();
             }
        });
    })
    

    heres a working JS Fiddle that will demonstrate this

    http://jsfiddle.net/HXYAD/

    However, its probably a good idea to abstract out the event handler for the click into a parameterized function so that you dont have

     jQuery('#btnOne').click(function(){
         //some code
     });
    
     jQuery('#btnTwo').click(function(){
         //some similar code
     });     
     jQuery('#btnThree').click(function(){
         //some more similar code
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a basic html/css question... I have a simple one-button form that
i have very simple basic form in HTML and i want to validate the
I have a basic html form that adds some data to the page using
I have the basic html form echoed through php: <html> <body> <?php if (isset($_GET[pk]))
Hey all, I have a basic HTML contact form that goes to a .php
I have a basic upload form <html> <head> <title>Upload a File</title> </head> <body> <font
Something strange is happening here. I have a basic form: <% using (Html.BeginForm()) {
Sorry about the wording for my question title. I have a basic HTML anchor
I have a very basic html, supposingly <html> <body> <iframe src=http://www.google.com> <p>Your browser does
Just a basic html link question. I have an intranet setup, and I need

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.