Skip to content

Instantly share code, notes, and snippets.

@faizaankhan
Created August 23, 2018 17:11
Show Gist options
  • Save faizaankhan/f5a0e133831c478d89d08049551cecfb to your computer and use it in GitHub Desktop.
Save faizaankhan/f5a0e133831c478d89d08049551cecfb to your computer and use it in GitHub Desktop.
$(document).on('turbolinks:load', function() {
console.log("may i come")
if ( $(".quantity").length > 0 ) {
updateQuantity();
};
});
updateQuantity = () => {
$(".quantity").bind('keyup change click mouseup', function() {
const quantity = $(".quantity").val();
console.log($(this).data("previousValue"))
if (! $(this).data("previousValue") || $(this).data("previousValue") != $(this).val()) {
$.ajax({
url: "/update_quantity",
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
type: "POST",
data: {id: $(this).attr('data-order_item-id'), quantity: quantity}
})
$(this).data("previousValue", $(this).val());
}
});
$(".quantity").each(function () {
$(this).data("previousValue", $(this).val());
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment