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

<link href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <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); }); });

Related: See More


Questions / Comments:

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?

Julie () - 1 year ago - Reply 0


Great snippet, but I found a bug with how the Javascript part is handled.

If you hold left click on + or -, then right click, move your mouse away from the + or - button and let go of both clicks, the "mouseup" function will never fire, and it will keep adding or substracting numbers and you can't stop it.

Olivier Mouvet () - 2 years ago - Reply 0


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">

davidgjm () - 3 years ago - Reply 0


To get it working on smartphones, you need to

.bind('touchstart', function() ...to the very same function as mousedown (copy-paste) and
.bind('touchend', function() ... to the very same function as mouseup (also copy-paste)

Works for me on an android 4.2 device. Any better solutions appreciated.

Phil () - 3 years ago - Reply 0


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() {

IMPORTANT: This solution works only on smartphones, so you can include device width check. It's very simple with jquery

Philip Fursov () - 1 year ago - Reply 0


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 () {

and this
}).mouseup(function(){
to this
}).on('mouseup touchend', function () {

Diego Engeles () - 3 years ago - Reply 0


This solution works sometimes

Philip Fursov () - 1 year ago - Reply 0


can you show your code with bind touch things?

Retina () - 3 years ago - Reply 0


I can write number more then max value, it's normal?

Bugs () - 3 years ago - Reply 0


it's not working correct on smartphones, you can't change the amount there (tested on iphone 6)
Could you fix this please?

Christian F () - 3 years ago - Reply 0