Problem with drag and drop an image to a cell table

Problem with drag and drop an image to a cell table

I'm making a little utility where the user is going to be able to drag an image and drop it inside of cell table. Everything works properly until the moment the image is dropped inside the cell, I want to have the image appended to the cell that was chosen for it, but it is appended into a completely diferent place.

HTML:
  1.    
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Test</title>
  6. </head>
  7. <script type="text/javascript" src="src/js/jquery-2.1.4.min.js"></script>
  8. <script type="text/javascript" src="src/js/jquery-ui.min.js"></script>
  9. <script type="text/javascript" src="src/js/futbolitoDados.js"></script>
  10. <link rel="stylesheet" type="text/css" href="src/css/boardStyle.css">
  11. <body>
  12. <div id="boardContainer">
  13. <div id="right">
  14. <p id="dice1"></p><p id="dice2"></p> 
  15. <button id="diceRoller">Lanzar Dados</button>
  16. </div>
  17. <div id="left"></div>
  18. </div>
  19. </body>
  20. <script type="text/javascript">
  21. var board = new Board();
  22. board.buildBoard('left',0,'black');
  23. </script>
  24. </html>

JavaScript:

  1. $(function() {
  2. $('td.soccer-field').click(function(){
  3. if ($('#dice1').text() != '' && $('#dice2').text() != '') {
  4. if ($('td[data-path="begin-path"]').length == 0) {
  5. $(this).attr('data-path','begin-path');
  6. $(this).off('mouseenter mouseleave');
  7. $(this).addClass('selected-path');
  8. } else if ($('td[data-path="begin-path"]').length && $(this).attr('data-path') != 'begin-path' && $('td[data-path="end-path"]').length == 0){
  9. $(this).attr('data-path','end-path');
  10. $(this).addClass('actual-path');
  11. $('.selected-path').addClass('actual-path');
  12. $('.actual-path').removeClass('selected-path');
  13. } else if ($(this).attr('data-path') == 'end-path'){
  14. $(this).removeAttr('data-path');
  15. $('.actual-path').addClass('selected-path');
  16. $('.selected-path').removeClass('actual-path');
  17. }; 
  18. }
  19. });


  20. $('div.soccer-field').hover(
  21. function () {
  22. if ($('#dice1').text() != '' && $('#dice2').text() != '') {
  23. if ($('td[data-path="begin-path"]').length == 0) {
  24. $(this).attr('data-path','select-begin-path');
  25. } else if($(this).attr('data-path') != 'begin-path'){
  26. var presentCell = $(this).attr('id') + $(this).parent().attr('id');
  27. var cell;
  28. if ($(this).attr('data-path') == 'ending') {
  29. $('.selected-path').each(function(){
  30. cell = $(this).attr('id') + $(this).parent().attr('id');
  31. distance = new Board().calculateDistanceSquares(presentCell,cell);
  32. if (distance == 1 ){
  33. $('div[data-path="ending"]').removeClass('selected-path');
  34. $('div[data-path="ending"]').removeAttr('data-path');
  35. if ($(this).attr('data-path') != 'begin-path')
  36. {
  37. $(this).attr('data-path','ending');
  38. }
  39. return;
  40. }
  41. });
  42. }else if ($('td[data-path="end-path"]').length == 0){
  43. var actualCell =$(this);
  44. var dicesResult = parseInt($('#dice1').text()) + parseInt($('#dice2').text());
  45. $('.selected-path').each(function(){
  46. cell = $(this).attr('id') + $(this).parent().attr('id');
  47. distance = new Board().calculateDistanceSquares(presentCell,cell);
  48. if (distance == 1 && $('.selected-path').length <= (dicesResult -1)){
  49. actualCell.attr('data-path','ending');
  50. actualCell.addClass('selected-path');
  51. if ($(this).attr('data-path') != 'begin-path') {
  52. $(this).removeAttr('data-path');
  53. };
  54. return ;
  55. }
  56. });
  57. }
  58. }
  59. }
  60. },function () {
  61. if ( $(this).attr('data-path') == "select-begin-path") {
  62. $(this).removeAttr('data-path');
  63. }
  64. });

  65. $('#diceRoller').click(function() {
  66. $('#dice1').text(Math.floor(Math.random()*6)+1);
  67. $('#dice2').text(Math.floor(Math.random()*6)+1);
  68. $(this).attr('disabled',true);
  69. });

  70. $('.drag-container img.draggable').draggable({
  71. cursor : 'move',
  72. revert: 'invalid',
  73. refreshPositions : true,
  74. opacity: .45,
  75. delay : 50
  76. });

  77. $('#board .soccer-field').droppable({
  78. accept: '.drag-container img.draggable',
  79. hoverClass: 'hover-pawn',
  80. drop: function(event, ui){
  81. $(this).html(ui.draggable);
  82. ui.draggable.draggable("option","disabled",true);
  83. }
  84. });
  85. });

  86. //function Board(playerTurn, piecesPosition, gamePhase, gameBegginingType, container)
  87. function Board(){
  88. this.buildBoard = function  (container,gamePhase,playerTurn) {
  89. var playerTurn = playerTurn;
  90. var gamePhase = gamePhase;
  91. var board = $('<table></table>').attr('id','board');
  92. if (gamePhase == 0) {
  93. var sparePiecesBlack = $('<div></div>').attr('id','spare-pieces-black'),img;
  94. sparePiecesBlack.addClass('drag-container');
  95. for (var i = 0; i < 5 ; i++) {
  96. img = $('<img>').attr('id',i+1);
  97. img.addClass('black draggable');
  98. img.attr('src','src/img/pieces/blackPawn.jpg');
  99. sparePiecesBlack.append(img);
  100. $("#diceRoller").attr('disabled',true);
  101. };
  102. $('#'+container).append(sparePiecesBlack);
  103. };
  104. var row, cell,containerHeight,containerWidth;
  105. for (var i=0; i<10; i++){
  106. row = $('<tr></tr>').attr('id',i+1);
  107. for (var j=0; j<20; j++){
  108. cell = $('<td></td>');
  109. if ((j == 0 || j == 19) && (i >= 3) && (i <= 6)) {
  110. cell.addClass('behind-goal');
  111. else if ((j > 0) && (j < 19)){
  112. cell.attr('id',String.fromCharCode(j+96));
  113. cell.addClass("soccer-field");
  114. };
  115. row.append(cell);
  116. }
  117. board.append(row);
  118. }
  119. $('#'+container).append(board);
  120. if (gamePhase == 0) {
  121. var sparePiecesWhite = $('<div></div>').attr('id','spare-pieces-white'),img;
  122. sparePiecesWhite.addClass('drag-container');
  123. for (var i = 0; i < 5 ; i++) {
  124. img = $('<img>').attr('id',i+1);
  125. img.addClass('white draggable');
  126. img.attr('src','src/img/pieces/whitePawn.jpg');
  127. sparePiecesWhite.append(img);
  128. };
  129. $('#'+container).append(sparePiecesWhite);
  130. };
  131. };

  132. this.calculateHorizontalDistance = function (sq1,sq2) {
  133. var column1 = sq1.substring(0,1).charCodeAt(0);
  134. var column2 = sq2.substring(0,1).charCodeAt(0);
  135. return ( Math.abs(column1-column2) );
  136. };

  137. this.calculateVerticalDistance = function (sq1, sq2) {
  138. var row1 = parseInt(sq1.substring(1));
  139. var row2 = parseInt(sq1.substring(1));
  140. return ( Math.abs(row1-row2) );
  141. };

  142. this.calculateDistanceSquares = function(sq1, sq2){
  143. return(this.calculateVerticalDistance(sq1,sq2)+this.calculateHorizontalDistance(sq1,sq2));
  144. }

  145. }

CSS:

  1. #left table{
  2. border:0.2em solid black;
  3. border-collapse:collapse;
  4. }

  5. #left table tr{
  6. height: 2.5em;
  7. }

  8. #left table tr td{
  9. width: 3.33em;
  10. }


  11. td.soccer-field{
  12. border: 0.1em solid black;
  13. }

  14. td.behind-goal{
  15. background-color: #F8FAB4;
  16. }

  17. .selected-path.soccer-field, td[data-path="select-begin-path"],.hover-pawn.soccer-field{
  18. border: 0.15em solid #F8FAB4;
  19. }

  20. .actual-path.soccer-field{
  21. border: 0.15em solid #F72D48;
  22. }

  23. .drag-container img:hover {
  24. cursor: move;
  25. }

  26. .drag-container{
  27. display: inline-block;
  28. }

  29. img {
  30. width : 3.33em;
  31. height: 2.5em
  32. }
Any help will be really appreciated