Easy Tutorial
❮ Api Addclass Api Tabbable Selector ❯

jQuery UI Example - Spinner

Enhance text input for numerical values with up/down buttons and arrow keys.

For more details about the spinner widget, please refer to the API documentation Spinner Widget.

Default Functionality

The default spinner.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Default Functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  <script>
  $(function() {
    var spinner = $( "#spinner" ).spinner();

    $( "#disable" ).click(function() {
      if ( spinner.spinner( "option", "disabled" ) ) {
        spinner.spinner( "enable" );
      } else {
        spinner.spinner( "disable" );
      }
    });
    $( "#destroy" ).click(function() {
      if ( spinner.data( "ui-spinner" ) ) {
        spinner.spinner( "destroy" );
      } else {
        spinner.spinner();
      }
    });
    $( "#getvalue" ).click(function() {
      alert( spinner.spinner( "value" ) );
    });
    $( "#setvalue" ).click(function() {
      spinner.spinner( "value", 5 );
    });

    $( "button" ).button();
  });
  </script>
</head>
<body>

<p>
  <label for="spinner">Select a value:</label>
  <input id="spinner" name="value">
</p>

<p>
  <button id="disable">Toggle disable/enable</button>
  <button id="destroy">Toggle widget</button>
</p>

<p>
  <button id="getvalue">Get value</button>
  <button id="setvalue">Set value to 5</button>
</p>


</body>
</html>

View Demo


Currency

This example is a donation form with currency selection and quantity spinner.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Currency</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
<script src="/static/js/jqueryui/resources/demos/external/globalize.js"></script>
<script src="/static/js/jqueryui/resources/demos/external/globalize.culture.de-DE.js"></script>
<script src="/static/js/jqueryui/resources/demos/external/globalize.culture.ja-JP.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
<script>
$(function() {
  $("#currency").change(function() {
    $("#spinner").spinner("option", "culture", $(this).val());
  });

  $("#spinner").spinner({
    min: 5,
    max: 2500,
    step: 25,
    start: 1000,
    numberFormat: "C"
  });
});
</script>
</head>
<body>

<p>
  <label for="currency">Currency for Donation</label>
  <select id="currency" name="currency">
    <option value="en-US">US $</option>
    <option value="de-DE">EUR €</option>
    <option value="ja-JP">YEN ¥</option>
  </select>
</p>
<p>
  <label for="spinner">Amount to Donate:</label>
  <input id="spinner" name="spinner" value="5">
</p>
</body>
</html>

View Demo


Decimal

This example is a decimal spinner. The increment is set to 0.01. The code handling cultural changes reads the current value of the selector, and when the culture changes, it re-formats the value based on the new culture.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Decimal</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/globalize.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.de-DE.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.ja-JP.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  <script>
  $(function() {
    $("#spinner").spinner({
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Decimal</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    var spinner = $( "#spinner" ).spinner({
      step: 0.01,
      numberFormat: "n"
    });

    $( "#culture" ).change(function() {
      var current = $( "#spinner" ).spinner( "value" );
      Globalize.culture( $(this).val() );
      $( "#spinner" ).spinner( "value", current );
    });
  });
  </script>
</head>
<body>

<p>
  <label for="spinner">Decimal Spinner:</label>
  <input id="spinner" name="spinner" value="5.06">
</p>
<p>
  <label for="culture">Select a culture for formatting:</label>
  <select id="culture">
    <option value="en-EN" selected="selected">English</option>
    <option value="de-DE">German</option>
    <option value="ja-JP">Japanese</option>
  </select>
</p>
</body>
</html>

View Demo


Map

Google Maps integration, using the spinner to change latitude and longitude.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Map</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  <script>
  $(function() {
    function latlong() {
      return new google.maps.LatLng( $("#lat").val(), $("#lng").val() );
    }
    function position() {
      map.setCenter( latlong() );
    }
    $( "#lat, #lng" ).spinner({
      step: .001,
      change: position,
      stop: position
    });

    var map = new google.maps.Map( $("#map")[0], {
      zoom: 8,
      center: latlong(),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  });
  </script>
  <style>
  #map {
    width:500px;
    height:500px;
  }
  </style>
</head>
<body>

<label for="lat">Latitude</label>
<input id="lat" name="lat" value="44.797">
<br>
<label for="lng">Longitude</label>
<input id="lng" name="lng" value="-93.278">

<div id="map"></div>
</body>
</html>

View Demo


Overflow

The overflow spinner limits the range from -10 to 10. For values above 10, it overflows to -10, and vice versa.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Overflow</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  <script>
  $(function() {
    $( "#spinner" ).spinner({
      spin: function( event, ui ) {
        if ( ui.value > 10 ) {
          $( this ).spinner( "value", -10 );
          return false;
        } else if ( ui.value < -10 ) {
          $( this ).spinner( "value", 10 );
          return false;
        }
      }
    });
  });
  </script>
</head>
<body>

<p>
  <label for="spinner">Select a value:</label>
  <input id="spinner" name="value">
</p>
</body>
</html>

View Demo


Time

A custom widget extending the spinner. Uses the Globalization plugin to parse and output timestamps, with custom step and page options. Up/Down arrows for incrementing/decrementing minutes, and Page Up/Down for incrementing/decrementing hours.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>jQuery UI Spinner - Time</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/jquery.mousewheel.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/globalize.js"></script>
  <script src="/static/js/jqueryui/resources/demos/external/globalize.culture.de-DE.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  <script>
  $.widget( "ui.timespinner", $.ui.spinner, {
    options: {
      // Seconds
      step: 60 * 1000,
      // Hours
<html>
<head>
  <script>
    $.widget( "ui.timespinner", $.ui.spinner, {
      options: {
        step: 60 * 1000,
        page: 60
      },

      _parse: function( value ) {
        if ( typeof value === "string" ) {
          // Already a timestamp
          if ( Number( value ) == value ) {
            return Number( value );
          }
          return +Globalize.parseDate( value );
        }
        return value;
      },

      _format: function( value ) {
        return Globalize.format( new Date(value), "t" );
      }
    });

    $(function() {
      $( "#spinner" ).timespinner();

      $( "#culture" ).change(function() {
        var current = $( "#spinner" ).timespinner( "value" );
        Globalize.culture( $(this).val() );
        $( "#spinner" ).timespinner( "value", current );
      });
    });
  </script>
</head>
<body>

<p>
  <label for="spinner">Time Spinner:</label>
  <input id="spinner" name="spinner" value="08:30 PM">
</p>
<p>
  <label for="culture">Select a culture for formatting:</label>
  <select id="culture">
    <option value="en-EN" selected="selected">English</option>
    <option value="de-DE">German</option>
  </select>
</p>
</body>
</html>

View Demo

❮ Api Addclass Api Tabbable Selector ❯