Skip to content

Instantly share code, notes, and snippets.

@GavinJaynes
Last active February 16, 2023 16:18
Show Gist options
  • Select an option

  • Save GavinJaynes/352507b7dd1123501acb to your computer and use it in GitHub Desktop.

Select an option

Save GavinJaynes/352507b7dd1123501acb 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);
});
}
@erwinprasetyo

Copy link
Copy Markdown

just want to say thanks, its really work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment