Note that there are some explanatory texts on larger screens.

plurals
  1. POUse UP/DOWN arrow keys to call a function (and passing parameter)
    primarykey
    data
    text
    <p>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:</p> <p>HTML:</p> <pre><code>&lt;div class="text-input-wrapper"&gt; &lt;input type="text" /&gt; &lt;div class="buttons inc-dec"&gt; &lt;button class="increment"&gt;Increment&lt;/button&gt; &lt;button class="decrement"&gt;Decrement&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; 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 </code></pre> <p>Now I need to enable incrementing and decrementing value using the UP &amp; DOWN keys; I took a stab with the following but with no joy:</p> <pre><code>$(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); </code></pre> <p>I’d greatly appreciate some clarification as to how to go about firing the increment/decrement using the up/down keys,</p> <p>Many thanks in advance, </p> <p>svs</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload