Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
               <div class="FormUnit FormUnit--spin js_spin">
	<input class="FormUnit-field FormUnit-field--spin js_spin-input" type="number" min="0" max="20" step="1" value="3" name="quantity" />
</div>
<div class="FormUnit js_spin FormUnit--spin">
	<input class="FormUnit-field FormUnit-field--spin js_spin-input" type="number" min="0" max="10" step="1" value="1" name="quantity" />
</div>
            
          
!
            
              .FormUnit--spin {
	display: inline-block;
	display: flex;
	margin-top: 0.5rem;
	margin-bottom: 0.5rem;
}
.FormUnit-quantity {
	background-color: #ddd;
	border: 1px solid #232323;
	height: 1.5rem;
}
.FormUnit-quantity--add {
	border-top-right-radius: 3px;
	border-bottom-right-radius: 3px;
	border-left: none;
	float: right;
	order: 3;
}
.FormUnit-quantity--remove {
	border-top-left-radius: 3px;
	border-bottom-left-radius: 3px;
	border-right: none;
	float: left;
	order: 1;
}
.FormUnit-field--spin {
	border: 1px solid #232323;
	order: 2;
	padding: 0.275rem;
	min-width: 1.5rem;
}

button:disabled {
	opacity: 0.5;
}

.FormUnit-field:invalid {
	border: 1px solid red;
}

.FormUnit-field:valid {
	border: 1px solid green;
}
            
          
!
            
              (function (win, doc) {
    'use strict';
    if (!doc.querySelector || !win.addEventListener || !doc.documentElement.classList) {
        return;
    }

    var Spinner = function(rootElement) {
        //Add button selector
        var addButtonSelector = '.js_spin-add';
        //Remove button selector
        var removeButtonSelector = '.js_spin-remove';
        //Number input selector
        var numberInputSelector = '.js_spin-input';
        //A variable to store the markup for the add button
        var addButtonMarkup = '<button type="button" class="FormUnit-quantity FormUnit-quantity--add js_spin-add Icon Icon--isClosed">Add</button>';
        //A variable to store the markup for the remove button
        var removeButtonMarkup = '<button type="button" class="FormUnit-quantity FormUnit-quantity--remove js_spin-remove Icon Icon--remove">Remove</button>';
        //Variable to store the root's container
        var container;
        //A variable for the markup of the number input pattern
        var markup;
        //A variable to store a number input
        var numberInput;
        //Variable to store the add button
        var addButton;
        //Variable to store the remove button
        var removeButton;
        //Store max value
        var maxValue;
        //Store min value
        var minValue;
        //Store step value
        var step;
        //Store new value
        var newValue;
        //Variable to store the loop counter
        var i;

        //Initialisation function
        this.init = function() {
            container = rootElement;
            //Get the markup inside the number input container
            markup = container.innerHTML;
            //Create a button to decrese the value by 1
            markup += removeButtonMarkup;
            //Create a button to increase the value by 1
            markup += addButtonMarkup;
            //Update the container with the new markup
            container.innerHTML = markup;

            //Get the add and remove buttons
            addButton = rootElement.querySelector(addButtonSelector);
            removeButton = rootElement.querySelector(removeButtonSelector);
            
            //Get the number input element
            numberInput = rootElement.querySelector(numberInputSelector);

            //Get min, max and step values
            if (numberInput.hasAttribute('max')) {
                maxValue = parseInt(numberInput.getAttribute('max'), 10);
            } else {
                maxValue = 99999;
            }
            if (numberInput.hasAttribute('min')) {
                minValue = parseInt(numberInput.getAttribute('min'), 10);
            } else {
                minValue = 0;
            }
            if (numberInput.hasAttribute('step')) {
                step = parseInt(numberInput.getAttribute('step'), 10);
            } else {
                step = 1;
            }

            //Change the number input type to text
            numberInput.setAttribute('type', 'text');
			
			//If there is there no pattern attribute, set it to only accept numbers
			if (!numberInput.hasAttribute('pattern')) {
				numberInput.setAttribute('pattern', '[0-9]');
			}

            //Add click events to the add and remove buttons
            addButton.addEventListener('click', add, false);  
            removeButton.addEventListener('click', remove, false);
        };

        //Methods for setting values
        this.setAddButtonMarkup = function(markup) {
            addButtonMarkup = markup;
        };

        this.setRemoveButtonMarkup = function(markup) {
            removeButtonMarkup = markup;
        };

        this.setAddButtonSelector = function(selector) {
            addButtonSelector = selector;
        };

        this.setRemoveSelector = function(selector) {
            removeButtonSelector = selector;
        };

        this.setNumberInputSelector = function(selector) {
            numberInputSelector = selector;
        };

        //Function to add one to the quantity value
        var add = function(ev) {
            newValue = parseInt(numberInput.value, 10) + step;
            //If the value is less than the max value
            if (newValue <= maxValue) {
                //Add one to the number input value
                numberInput.value = newValue;
                //Button is active
                removeButton.disabled = false;
            }
            //If the value is equal to the max value
            if (numberInput.value == maxValue || newValue > maxValue) {
                //Disable the button
                addButton.disabled = true;
            }
            ev.preventDefault();
        };
        //Function to subtract one from the quantity value
        var remove = function(ev) {
            newValue = parseInt(numberInput.value, 10) - step;
            //If the number input value is bigger than the min value, reduce the the value by 1
            if (newValue >= minValue) {
                numberInput.value = newValue;
                addButton.disabled = false;
            }
            //If the input value is the min value, add disabled property to the button
            if (numberInput.value == minValue || newValue < minValue) {
                removeButton.disabled = true;
            }
            ev.preventDefault();
        };
    };

    //Get all of the number input elements
    var spins = doc.querySelectorAll('.js_spin');
    //Store the total number of number inputs
    var spinsTotal = spins.length;
    //A variable to store one number inputs
    var spin;
    //A counter for the loop
    var i;
    //Loop through each number input
    for ( i = 0; i < spinsTotal; i = i + 1 ) {
        //Create a new Spin object for each number input
        spin = new Spinner(spins[i]);
        //Start the initialisation function
        spin.init();
    }

}(this, this.document));
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console