Afzal Khan
Afzal Khan

Reputation: 125

How to increment of 1 after 5th zero?

I'm trying to increment of 1 after 5th zero but when I click the up button it shows me just 1 rather 000001.

<div class="input-group spinner">
    <input type="text" class="form-control" value="000001">
    <div class="input-group-btn-vertical">
       <button class="btn btn-default" type="button"><i class="fa fa-caret-up"></i></button>
       <button class="btn btn-default" type="button"><i class="fa fa-caret-down"></i></button>
   </div>
</div>

jQuery

<script>                          
(function ($) {
   $('.spinner .btn:first-of-type').on('click', function() {
      $('.spinner input').val( parseInt($('.spinner input').val(), 10) + 1);
   });
   $('.spinner .btn:last-of-type').on('click', function() {
      $('.spinner input').val( parseInt($('.spinner input').val(), 10) - 1);
   });
})(jQuery);
</script>           
                

Upvotes: 1

Views: 54

Answers (4)

NID
NID

Reputation: 3294

Use the String.slice method as follows:

Here n is the number

var output = [], n, padded;
for (n=0; n<=9999; n++) {
    padded = ('0000'+n).slice(-4); // Prefix three zeros, and get the last 4 chars
    output.push(padded);
}
console.log(output); // ["0000", "0001", ..., "9999"]

http://jsfiddle.net/qJSBg/

Upvotes: 0

vijayP
vijayP

Reputation: 11502

You may look at below code. Actually the issue was with parseInt. If you execute over the string like 000002 it will give you a result of 2. So after doing mathematical calculation we again need to formulate the required string by prepending the zeros as per length:

(function($) {
  $('.spinner .btn:first-of-type').on('click', function() {
    $('.spinner input').val(prependZero(parseInt($('.spinner input').val(), 10) + 1, 6));
  });
  $('.spinner .btn:last-of-type').on('click', function() {
    $('.spinner input').val(prependZero(parseInt($('.spinner input').val(), 10) - 1, 6));
  });

  function prependZero(num, size) {
    if (num <= 0)
      num = 0;
    var s = num + "";
    while (s.length < size) s = "0" + s;
    return s;
  }

})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="input-group spinner">
  <input type="text" class="form-control" value="000001">
  <div class="input-group-btn-vertical">
    <button class="btn btn-default" type="button"><i class="fa fa-caret-up"></i></button>
    <button class="btn btn-default" type="button"><i class="fa fa-caret-down"></i></button>
  </div>
</div>

Upvotes: 1

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

Try like this

  var noOfZer = 6; // Total length of number, for you number is 6
  var num=parseInt($('.spinner input').val(), 10) - 1;
  console.log(String('000000' + num).slice(-1 * noOfZer));

demo

Upvotes: 1

geekbro
geekbro

Reputation: 1323

I think you should try

 $('.spinner input').val( "00000"+(parseInt($('.spinner input').val(), 10) + 1));

and

 $('.spinner input').val( "00000"+(parseInt($('.spinner input').val(), 10) - 1));

because parseInt() returns just the integer value, which is excluding zeros

Upvotes: 1

Related Questions