Skip to main content

Drag and Drop Shopping Cart using JQuery

Language


In this project you are going to learn drag and drop feature in shopping cart using JQuery. This Drag and Drop Shopping Cart feature can be useful to your website or project.

Source Code:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Shopping Cart</title>
  6. <link rel="stylesheet" href="jquery-ui.css">
  7. <script src="jquery-1.10.2.js"></script>
  8. <script src="jquery-ui.js"></script>
  9. <style>
  10. h1 { padding: .2em; margin: 0; }
  11. #products { float:left; width: 400px; margin-right: 2em; }
  12. #cart { width: 230px; float: left; margin-top: 1em; }
  13. /* style the list to maximize the droppable hitarea */
  14. #cart ol { margin: 0; padding: 1em 0 1em 3em; }
  15. </style>
  16. <script>
  17. $(function() {
  18. $( "#catalog" ).accordion();
  19. $( "#catalog li" ).draggable({
  20. appendTo: "body",
  21. helper: "clone"
  22. });
  23. $( "#cart ol" ).droppable({
  24. activeClass: "ui-state-default",
  25. hoverClass: "ui-state-hover",
  26. accept: ":not(.ui-sortable-helper)",
  27. drop: function( event, ui ) {
  28. $( this ).find( ".placeholder" ).remove();
  29. $( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
  30. }
  31. }).sortable({
  32. items: "li:not(.placeholder)",
  33. sort: function() {
  34. $( this ).removeClass( "ui-state-default" );
  35. }
  36. });
  37. });
  38. </script>
  39. </head>
  40. <body>
  41.  
  42. <div id="products">
  43. <h1 class="ui-widget-header">Products</h1>
  44. <div id="catalog">
  45. <h2><a href="#">Gadgets</a></h2>
  46. <div>
  47. <ul>
  48. <li>iPhone</li>
  49. <li>iPod</li>
  50. <li>iPad</li>
  51. </ul>
  52. </div>
  53. <h2><a href="#">Accessories</a></h2>
  54. <div>
  55. <ul>
  56. <li>Earphone</li>
  57. <li>USB Cord</li>
  58. <li>Powerbank</li>
  59. </ul>
  60. </div>
  61. <h2><a href="#">Bags</a></h2>
  62. <div>
  63. <ul>
  64. <li>Zebra Striped</li>
  65. <li>Black Leather</li>
  66. <li>Alligator Leather</li>
  67. </ul>
  68. </div>
  69. </div>
  70. </div>
  71.  
  72. <div id="cart">
  73. <h1 class="ui-widget-header">Shopping Cart</h1>
  74. <div class="ui-widget-content">
  75. <ol>
  76. <li class="placeholder">Add your items here</li>
  77. </ol>
  78. </div>
  79. </div>
  80.  
  81.  
  82. </body>
  83. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.