Skip to content

Instantly share code, notes, and snippets.

@Tusko
Last active June 23, 2016 13:03
Show Gist options
  • Select an option

  • Save Tusko/6507eb3c36e7f7d89d8ca4c5fb4b268c to your computer and use it in GitHub Desktop.

Select an option

Save Tusko/6507eb3c36e7f7d89d8ca4c5fb4b268c to your computer and use it in GitHub Desktop.
Is Numberic input
.input-number-box {
@include transform(translate3d(0,0,0));
@include border-radius(5px);
position: relative;
background: #000;
overflow: hidden;
text-align: left;
width: 100px;
* {@include transform(translate3d(0,0,0));}
input {
-webkit-appearance: textfield;
-moz-appearance:textfield;
position: relative;
text-align: center;
background: #fff;
display: block;
color: #000;
height: 50px;
z-index: 99;
width: 50%;
border: 0;
&::-webkit-outer-spin-button,
&::-webkit-inner-spin-button {
-webkit-appearance: none;
}
}
> i {
@include transform(translate(50%, -125%));
@include transition();
position: absolute;
height: 10px;
width: 14px;
right: 25%;
top: 50%;
&:before {
@include triangle(5px, 7px, #fff, 'up');
display: block !important;
margin: 3px auto;
content: '';
}
&.input-number-less {
@include transform(translate(50%, 25%));
&:before {
@include triangle(5px, 7px, #fff, 'down');
}
}
&:hover {
cursor: pointer;
opacity: .5
}
}
}
function is_numeric_input(){
'use strict';
$('.is_numeric').each(function(){
var t = $(this);
if(t.parent().is('.input-number-box')) {
return;
}
if(mob()) {
t.attr('type', 'number').removeAttr('onkeyup');
}
t.wrap('<div class="input-number-box" />').after('<i class="input-number-more" /><i class="input-number-less" />');
});
}
$(document).on('click', '.input-number-box i', function(){
var t = $(this),
input = t.siblings('input'),
val = Number(input.val());
if(t.hasClass('input-number-more')) {
parseInt(input.val(val + 1), 1);
} else if(t.hasClass('input-number-less') && val.length !== '' && val > 1) {
parseInt(input.val(val - 1), 1);
}
return false;
});
<input class="is_numeric" name="quantity" value="1" type="text" autocomplete="off" onkeyup="this.value = !isNaN(parseInt(this.value)) ? parseInt(this.value) : ''"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment