Need help to create function of drag div or text in jquery.

Need help to create function of drag div or text in jquery.

Hi all,

I am using jquery for draging a div. still it working on div id base but i want use as function like mydrag('div1'){ // } please help me..

  1. <html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  3. <title>Test File</title>

  4. <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  5. <script type="text/javascript" src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>

  6. <style type="text/css">
  7. #canvas { width: 500px; height: 400px; background: #ccc; position: relative; margin: 2em auto; }
  8. #box { position: absolute; top: 0; left: 0; width: 100px; height: 100px; background: green; cursor: move; }
  9. </style>

  10. <script type="text/javascript">//<![CDATA[ 
  11. $(function(){
  12. var coordinates = function(element) {
  13. element = $(element);
  14. var top = element.position().top;
  15. var left = element.position().left;
  16. $(element).text('X: ' + left + ' ' + 'Y: ' + top);
  17. }

  18. function mydrag(id){
  19. $(id).draggable({
  20. start: function() {
  21. coordinates(id);
  22. },
  23. containment : "#canvas",

  24. stop: function() {
  25. coordinates(id);
  26. }
  27. });
  28. }

  29. });//]]>  

  30. </script>


  31. </head>

  32. <body style="cursor: auto;">
  33. <div id="canvas">
  34. <div id="box"  style="left: 189px; top: 134px;" onclick="mydrag('box');" ></div>
  35. </div>
  36. </body>
  37. </html>