Try It Editor
Tip: Press
Ctrl/⌘ + Enter
to Run
▶ Run
Reset
Copy
Download
Code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>jQuery UI - Droppable Accept Filter</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script> <style> body{font-family:Arial, sans-serif; padding:20px;} #wrap{display:flex;gap:24px;align-items:flex-start;flex-wrap:wrap;margin-bottom:20px;} .drag{width:100px;height:60px;color:#fff;border-radius:6px;display:flex;align-items:center;justify-content:center;cursor:move;text-align:center;} .fruit{background:#04AA6D;} .veggie{background:#e67e22;} #cart{width:180px;height:120px;border:2px dashed #999;border-radius:6px;background:#fafafa;display:flex;align-items:center;justify-content:center;text-align:center;color:#666;} </style> </head> <body> <h3>accept: '.fruit' - cart only accepts fruit items</h3> <div id="wrap"> <div class="drag fruit">Apple (fruit)</div> <div class="drag veggie">Carrot (veggie)</div> </div> <div id="cart">Cart (fruit only)</div> <script> $(document).ready(function(){ $(".drag").draggable({ revert: "invalid", helper: "clone" }); $("#cart").droppable({ accept: ".fruit", drop: function(event, ui){ $(this).text("Added: " + ui.draggable.text()); } }); }); </script> </body> </html>
Output