jquery.input-stepper.js examples

https://github.com/AanZee/input-stepper

by Jeroen Ransijn

HTML

<h1>jquery.input-stepper.js</h1>

	<h2>Basic usage</h2>

	<div class="input-stepper">
		<button data-input-stepper-decrease>-</button>
		<input type="text">
		<button data-input-stepper-increase>+</button>
	</div>

	<hr>

	<h2>Basic usage with links</h2>
	<p>Increase/decrease elements always get a classs when disabled, by default this is <code>is-disabled</code></p>

	<div class="input-stepper">
		<a href="#" data-input-stepper-decrease>Decrease</a>
		<input type="text">
		<a href="#" data-input-stepper-increase>Increase</a>
	</div>

	<hr>

	<h2>With intial value</h2>

	<div class="input-stepper">
		<button data-input-stepper-decrease>-</button>
		<input type="text" value="6">
		<button data-input-stepper-increase>+</button>
	</div>

	<hr>

	<h2>With min and max value</h2>

	<div class="input-stepper">
		<button data-input-stepper-decrease>-</button>
		<input type="text" min="36" max="52">
		<button data-input-stepper-increase>+</button>
	</div>

	<hr>

	<h2>With configured increase/decrease values and max value</h2>

	<div class="input-stepper">
		<button data-input-stepper-decrease="10">-10</button>
		<button data-input-stepper-decrease="5">-5</button>
		<button data-input-stepper-decrease>-1</button>
		<input type="text" max="100">
		<button data-input-stepper-increase>+1</button>
		<button data-input-stepper-increase="5">+5</button>
		<button data-input-stepper-increase="10">+10</button>
	</div>

	<hr>

	<h2>Advanced usage with settings</h2>

	<div class="input-stepper-advanced">
		<button class="decrease" data-decrease="10">-10</button>
		<button class="decrease" data-decrease="5">-5</button>
		<button class="decrease" data-decrease>-1</button>
		<input type="text" value="50" min="25" max="75">
		<button class="increase" data-increase>+1</button>
		<button class="increase" data-increase="5">+5</button>
		<button class="increase" data-increase="10">+10</button>
	</div>

	<hr>

	<h2>Custom increase/decrease events on the input element</h2>

	<div...

JavaScript

// jquery.input-stepper.js
// ------------------------------------------------------
// Author: Jeroen Ransijn - Aan Zee
;(function (root, $, undefined) {

	var pluginName = "inputStepper";
	var defaults = {
		selectorButtonIncrease: '[data-input-stepper-increase]',
		selectorButtonDecrease: '[data-input-stepper-decrease]',
		dataAttributeIncrease: 'input-stepper-increase',
		dataAttributeDecrease: 'input-stepper-decrease',
		classNameDisabled: 'is-disabled',
		selectorInput: 'input',
		initialValue: null,
		min: 0,
		max: Infinity
	};

	// The actual plugin constructor
	function Plugin( element, options ) {
		this.element = element;

		this.options = $.extend( {}, defaults, options) ;

		this._defaults = defaults;
		this._name = pluginName;

		return this.init();
	}

	Plugin.prototype = {

		init: function () {
			var _this = this;

			// Cache elements
			this.$el = $(this.element);
			this.$input = this.$el.find(this.options.selectorInput);
			this.$decreaseButton = this.$el.find(this.options.selectorButtonDecrease);
			this.$increaseButton = this.$el.find(this.options.selectorButtonIncrease);

			this.min = this.$input.attr('min') || this.options.min;
			this.max = this.$input.attr('max') || this.options.max;

			this.initialValue = this.getValue() || this.options.initialValue || this.min;

			this.setValue(this.initialValue);
			this.checkState();

			this.$input.attr('pattern', '[0-9]*');

			// Bind events
			this.$input.on('keydown', this.onKeyDown.bind(this) );

			this.$input.on('change', this.onChange.bind(this) );

			this.$increaseButton.on('click', function (e) {
				e.preventDefault();
				_this.increase( $(this).data( _this.options.dataAttributeIncrease ) || 1 );
			});
			this.$decreaseButton.on('click', function (e) {
				e.preventDefault();
				_this.decrease( $(this).data('input-stepper-decrease') || 1 );
			});

			return this;
		},

		onChange: function (e) {
			if (this.getValue() === '') {
				this.setValue( this.initialValue );
			} else...