"Bootstrap Number Spinner On Click Hold"
Bootstrap 3.2.0 Snippet by onro

<div class="container"> <div class="row"> <div class="col-xs-3 col-xs-offset-3"> <p>Min: -10</p> <p>Max: 40</p> </div> </div> <div class="row"> <div class="col-xs-3 col-xs-offset-3"> <div class="input-group number-spinner"> <span class="input-group-btn data-dwn"> <button class="btn btn-default btn-info" data-dir="dwn"><span class="glyphicon glyphicon-minus"></span></button> </span> <input type="text" class="form-control text-center" value="1" min="-10" max="40"> <span class="input-group-btn data-up"> <button class="btn btn-default btn-info" data-dir="up"><span class="glyphicon glyphicon-plus"></span></button> </span> </div> </div> </div> <div class="row"> <div class="col-xs-3 col-xs-offset-3"> <p>It's responsive too!</p> </div> </div> </div>
/* just for preview */ .container { padding-top: 40px; } @media ( max-width: 585px ) { .input-group span.input-group-btn,.input-group input,.input-group button{ display: block; width: 100%; border-radius: 0; margin: 0; } .input-group { position: relative; } .input-group span.data-up{ position: absolute; top: 0; } .input-group span.data-dwn{ position: absolute; bottom: 0; } .form-control.text-center { margin: 34px 0; } .input-group-btn:last-child>.btn, .input-group-btn:last-child>.btn-group{ margin-left:0; } }
$(function() { var action; $(".number-spinner button").mousedown(function () { btn = $(this); input = btn.closest('.number-spinner').find('input'); btn.closest('.number-spinner').find('button').prop("disabled", false); if (btn.attr('data-dir') == 'up') { action = setInterval(function(){ if ( input.attr('max') == undefined || parseInt(input.val()) < parseInt(input.attr('max')) ) { input.val(parseInt(input.val())+1); }else{ btn.prop("disabled", true); clearInterval(action); } }, 50); } else { action = setInterval(function(){ if ( input.attr('min') == undefined || parseInt(input.val()) > parseInt(input.attr('min')) ) { input.val(parseInt(input.val())-1); }else{ btn.prop("disabled", true); clearInterval(action); } }, 50); } }).mouseup(function(){ clearInterval(action); }); });

Similar snippets: See More


Comments:

Christian F 2014-12-09 16:32:10
    it's not working correct on smartphones, you can't change the amount there (tested on iphone 6)
Could you fix this please?
Bugs 2015-01-15 13:17:51
    I can write number more then max value, it's normal?
Phil 2015-02-03 20:52:40
    To get it working on smartphones, you need to
Retina 2015-02-15 16:01:23
    can you show your code with bind touch things?
Diego Engeles 2015-02-25 08:11:23
    It's not so good solution. Just try it:
replace this
$(".number-spinner button").mousedown( function () {
to this
$(".number-spinner button").on('mousedown touchstart', function () {
Philip Fursov 2016-08-20 16:13:59
    This solution works sometimes
Philip Fursov 2016-08-20 16:18:31
    This is works good. full rows are:
instead of $(".number-spinner button").mousedown(function () {
use
$(".number-spinner button").bind('touchstart', function() {
and instead of
}).mouseup(function(){
use
}).bind('touchend', function() {
davidgjm 2015-02-17 04:08:49
    This can be improved if a pattern is applied. My version would look like below:
<input type="text" class="form-control text-center" pattern="[1-5]" value="1" min="1" max="5">
Olivier Mouvet 2016-07-14 09:01:51
    Great snippet, but I found a bug with how the Javascript part is handled.
Julie 2016-11-22 04:09:40
    It doesn't seem to work (buttons refresh the page and the numbers don't increase/decrease) when the input-group is inside a bootstrap modal. Can anyone help me with this?

Commenting will be back soon.