Skip to content

Instantly share code, notes, and snippets.

@marshallm
Created April 20, 2015 19:36
Show Gist options
  • Save marshallm/72a7361dfd63e52884e8 to your computer and use it in GitHub Desktop.
Save marshallm/72a7361dfd63e52884e8 to your computer and use it in GitHub Desktop.
jquery
tolerance:"touch",
drop:function (event, ui) {
var basket = $(this),
move = ui.draggable,
itemId = basket.find("ul li[data-id='" + move.attr("data-id") + "']");
// To increase the value by +1 if the same item is already in the basket
if (itemId.html() != null) {
itemId.find("input").val(parseInt(itemId.find("input").val()) + 1);
}
else {
// Add the dragged item to the basket
addBasket(basket, move);
// Updating the quantity by +1" rather than adding it to the basket
move.find("input").val(parseInt(move.find("input").val()) + 1);
}
}
});
// This function runs onc ean item is added to the basket
function addBasket(basket, move) {
basket.find("ul").append('<li data-id="' + move.attr("data-id") + '">'
+ '<span class="name">' + move.find("h4").html() + '</span>'
+ '<input class="count" value="1" type="text">'
+ '<input class="price" value="1" type="text">'
+ '<button class="delete">&#10005;</button>');
}
// The function that is triggered once delete button is pressed
$(".basket ul li button.delete").live("click", function () {
$(this).closest("li").remove();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment