Creating a shopping cart from the Draggable
Hi all, -> demo
http://bit.ly/9WxRiY
I am having problems with using the draggable shopping cart. The value is automatically being added to the total before the item has been dragged to the required area. However, when I add an alert, the alert isnt called until i drag. Which confuses me to why would the value be added to the total? :-/
Firstly I have created a function which updates the Total. 'input:checked, .input2,' are checkboxes and radio buttons that update on click
- function calcTotal()
- {
- var total = 0;
- $("input:checked, .input2, li.test2").each(function()
- {
-
- //This happens for each checked input field
- var value = $(this).attr("value");
- //alert(value);
- total += parseInt(value);
- });
- $("p.total").html("Total: <strong>" + total + "</strong>");
- }
Secondly, I have then added the function calcTotal() to the draggable code:
- $("#catalog li").draggable({
- appendTo: "body",
- helper: "clone"
- });
- $("#cart ol").droppable({
- activeClass: "ui-state-default",
- hoverClass: "ui-state-hover",
- accept: ":not(.ui-sortable-helper)",
- drop: function(event, ui) {
- $(this).find(".placeholder").remove();
- $("<li></li>").text(ui.draggable.text()).appendTo(this);
- //alert('handle');
- calcTotal()
- }
- }).sortable({
- items: "li:not(.placeholder)",
- sort: function() {
- // gets added unintentionally by droppable interacting with sortable
- // using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
- $(this).removeClass("ui-state-default");
- }
- });
HTML code for the drag-drop:
- <div id="products">
- <h1 class="ui-widget-header">Products</h1>
- <div id="catalog">
- <h3><a href="#">T-Shirts</a></h3>
- <div>
- <ul>
- <li class="test2" value="0">test</li>
- <li class="test2" value="0">Cheezeburger Shirt</li>
- <li class="test2" value="1">Buckit Shirt</li>
- </ul>
- </div>
- <h3><a href="#">Bags</a></h3>
- <div>
- <ul>
- <li>Zebra Striped</li>
- <li>Black Leather</li>
- <li>Alligator Leather</li>
- </ul>
- </div>
- <h3><a href="#">Gadgets</a></h3>
- <div>
- <ul>
- <li>iPhone</li>
- <li>iPod</li>
- <li>iPad</li>
- </ul>
- </div>
- </div>
- </div>
-
- <div id="cart">
- <h1 class="ui-widget-header">Shopping Cart</h1>
- <div class="ui-widget-content">
- <ol>
- <li class="placeholder">Add your items here</li>
- </ol>
- </div>
- </div>
All advice welcome
Thank you