Skip to content

Instantly share code, notes, and snippets.

@erwinprasetyo
Forked from GavinJaynes/number-input-tweak.js
Created December 24, 2016 06:49
Show Gist options
  • Save erwinprasetyo/39758682e1cf6f714b2ebb45b60a00a3 to your computer and use it in GitHub Desktop.
Save erwinprasetyo/39758682e1cf6f714b2ebb45b60a00a3 to your computer and use it in GitHub Desktop.
Change WooCommerce number input ( Quantity ) to use custom styles using JavaScript. This can be used for any number field with the step attribute.
// Nominate the containing selector
var parentSelector = $('.quantity');
// If it's on the page
if( parentSelector.length ) {
// Get the original HTML
var numberInputs = parentSelector.html();
// Change number to text
var textInputs = numberInputs.replace('type="number"', 'type="text"');
// Plus button
var btnMore = '<button class="more">+</button>';
// Minus button
var btnLess = '<button class="less">-</button>';
// Append it all
parentSelector.append(textInputs + btnMore + btnLess);
// Hide the original
parentSelector.find('input[type="number"]').hide();
// increase or decrease the count
$('.more, .less').on('click', function(e) {
e.preventDefault();
var newCounter = $(this).prevAll('input.qty[type="text"]');
var oldCounter = $(this).prevAll('input.qty[type="number"]');
var counterVal = newCounter.val();
if( $(e.target).hasClass('more') ) {
counterVal++ ;
} else {
counterVal-- ;
}
// Apply to both inputs
newCounter.val(counterVal);
oldCounter.val(counterVal);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment