JSFiddle - React, Tailwind, and code Playground
by Karmik Patel
HTML
<div class="spin"><span>–</span><input value="0" /><span>+</span></div>
CSS
.spin {
display: inline-block;
}
.spin span {
display: inline-block;
width: 20px;
height: 22px;
text-align: center;
padding-top: 2px;
background: #ff0;
border: 1px solid #aaa;
border-radius: 0 4px 4px 0;
cursor: pointer;
-webkit-user-select: none;
-webkit-user-modify: read-only !important;
}
.spin span:first-child {
border-radius: 4px 0 0 4px;
}
.spin input {
width: 40px;
height: 20px;
text-align: center;
font-weight: bold;
}
JavaScript
var spins = document.getElementsByClassName("spin");
for (var i = 0, len = spins.length; i < len; i++) {
var spin = spins[i],
span = spin.getElementsByTagName("span"),
input = spin.getElementsByTagName("input")[0];
input.onchange = function() { input.value = +input.value || 0; };
span[0].onclick = function() { input.value = Math.max(0, input.value - 1); };
span[1].onclick = function() { input.value -= -1; };
}