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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:00:16+00:00 2026-06-13T06:00:16+00:00

I have a survey in which there are numerous text inputs taking numerical value

  • 0

I have a survey in which there are numerous text inputs taking numerical value each of which has a pair of increment/decrement buttons. I have a working function as follows:

HTML:

<div class="text-input-wrapper">
    <input type="text" />
    <div class="buttons inc-dec">
        <button class="increment">Increment</button>
        <button class="decrement">Decrement</button>
    </div>
</div>

function incDec(btnClass)   {

    var input       = $(this).closest('.text-input-wrapper').find('input');
    var inputVal    = $(this).closest('.text-input-wrapper').find('input').val();

    if ($(this).hasClass('increment')) {
        if (inputVal.length == 0) {
            ($(input).val('1'));
        }
        else {
            inputVal++;
            $(input).val(inputVal);
        }
    } // increment

    else if ($(this).hasClass('decrement')) {
        if (inputVal.length == 0) {
            ($(input).val('0'));
        }
        else {
            inputVal--;
            $(input).val(inputVal);
        }
    } // decrement

    return false;

} // function incDec

Now I need to enable incrementing and decrementing value using the UP & DOWN keys; I took a stab with the following but with no joy:

$(document).keydown( function(eventObject) {
    // UP arrow
    if(eventObject.which==38) {
        var btnClass = "increment";
    } else

    // DOWN arrow
    if(eventObject.which==40) {
        var btnClass = "decrement";
    }
    incDec(btnClass);

I’d greatly appreciate some clarification as to how to go about firing the increment/decrement using the up/down keys,

Many thanks in advance,

svs

  • 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-13T06:00:18+00:00Added an answer on June 13, 2026 at 6:00 am

    Your problem is that your incDec function is not designed to take the class as an input parameter – it uses the object that calls the event (i.e. the click event on a button).

    It’s a bit hacky, but how about something like below? You assign the click handler to each button then invoke the relevant button’s click-event.

    $( function ( ) {
        $('button').click( incDec );
    
        $('input').keydown( function(eventObject) {
    
            // UP arrow
            if(eventObject.which==38) {
                var btnClass = "increment";
            } else
    
            // DOWN arrow
            if(eventObject.which==40) {
                var btnClass = "decrement";
            }
    
            // Invoke the click event for the inc/dec button inside the containing div.
            $('button.'+btnClass, $(this).parent( )).click( );
        });
    });
    

    Here’s a jsfiddle with two such boxes. when you use the up/down arrow keys while inside a text-box it increments/decrements correctly. http://jsfiddle.net/zJBEN/

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

Sidebar

Related Questions

Have a Django / MySQL set up. There's a model, Survey, which currently looks
I have an online survey dataset in which there are multiple complete attempts by
Is there a rails helper for this case? You have a Survey , which
I have a JavaScript code which links to survey poll page. When I am
I have a django app which is used for managing registrations to a survey.
I have a survey on a website, and there seems to be some issues
I have a survey database with one column for each question and one row
I have recently written a survey application that has done it's job and all
I have a table which contains asp radiobuttonlists (see below) for a survey. Once
I have an aspx form in which user takes a survey and the survey

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.