jquery input number

44,813

Solution 1

$("#minus,#plus").click(function(){
    var value = parseInt($(".numbertype").val(), 10);
    $(".numbertype").val(value + $(this).is("#minus") ? -1 : 1);
});

I just wanted to see how to do it myself. Here's a toggle that will keep going on mouse down:

var i = null;
var d = 0;
var $numbertype = null;

function ToggleValue() {
    $numbertype.val(parseInt($numbertype.val(), 10) + d);
}

$(function() {
    $numbertype = $(".numbertype");

    $("#minus,#plus").mousedown(function() {
        d = $(this).is("#minus") ? -1 : 1;
        i = setInterval(ToggleValue, 100);
    });

    $("#minus,#plus").on("mouseup mouseout", function() {
        clearInterval(i);
    });
});

working example: http://jsfiddle.net/M4BAt/5/

Solution 2

If you substitute the images you want for the plus/minus buttons, this would work:

<span class="plusminusunit">
    <span class="minus">-</span> 
    <input type="number" class="numbertype" value="10">
    <span class="plus">+</span>
</span>

$(".plus").click(function() {
    var item = $(this).closest(".plusminusunit").find("input");
    item.val(Number(item.val()) + 1); 
});
$(".minus").click(function() {
    var item = $(this).closest(".plusminusunit").find("input");
    item.val(Number(item.val()) - 1); 
});

Working example: http://jsfiddle.net/jfriend00/FxEv3/

With images, the HTML would look like this (using your image URLs):

<span class="plusminusunit">
    <img class="minus" src="http://lt.tonybet.com/assets/shared/mikro_tv-50ee396d59d5c38f979ad3fd93578f4f.png"> 
    <input type="number" class="numbertype" value="10">
    <img class="plus" src="http://lt.tonybet.com/assets/shared/mikro_tv-50ee396d59d5c38f979ad3fd93578f4f.png">
</span>
Share:
44,813
robpal
Author by

robpal

Updated on February 03, 2020

Comments

  • robpal
    robpal over 4 years

    Possible Duplicate:
    Is it possible to customize an input field for amounts with +- buttons?

    how to make + (plus) and - (minus) value buttons for

    <input type="number" class="numbertype" value="10"> ?

    I think it's possible to do with jQuery or maybe simple javascipt, but i don't know how..

    I want do something like this :

    Input tyoe number with + and -

    when you push on + button, value will be bigger for 1 (0,12,3,4,5,6.... 10000)

    when you push on - button, value will be smaller for 1 (10,9,8,7,6,5,4... 0)