How to clone the highlighted row data to another table when ENTER key is pressed.

How to clone the highlighted row data to another table when ENTER key is pressed.

I am having a html data table with a highlight class to highlight the rows when I press the up-down arrow keys when the table is in focus. I have another table which is blank and I want to add the highlighted rows to the 2nd table when I press the ENTER key. after many tries I am not getting what I want. This is my code what I have tried :

  1. <html><head><title>dynamictable</title>      
  2. <style>
  3. table { cursor: pointer; }
  4. .highlight { background-color: lightgreen; }
  5. </style>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <table id="myTable" border="1px" style="width: 30%;">
  10. <thead>
  11.   <tr>
  12. <th>##</th>
  13. <th>Name</th>
  14. <th>code</th>
  15. <th>unit</th>
  16. <th>rate</th>
  17.   </tr>
  18. </thead>
  19. <tbody>
  20.   <tr>
  21. <td>1</td>
  22. <td>aaa</td>
  23. <td>aa1</td>
  24. <td>aa</td>
  25. <td>111</td>
  26.   </tr>
  27. <tr>
  28. <td>1</td>
  29. <td>aaa</td>
  30. <td>aa1</td>
  31. <td>aa</td>
  32. <td>111</td>
  33.   </tr>
  34. </tbody>
  35.   </table>
  36.   
  37. <br>
  38. <br>
  39. <br>
  40. <input type="button" id="goto_first" value="first" class="btn btn-success">
  41. <input type="button" id="goto_prev" value="prev" class="btn btn-secondary">
  42. <input type="button" id="goto_next" value="next" class="btn btn-secondary">
  43. <input type="button" id="goto_last" value="last" class="btn btn-success">
  44. <br>
  45. <br>
  46. <br>

  47.   <table id="cloneTable" border="1px" style="width: 30%; float :left;"><!--new table to clone data-->
  48. <thead>
  49.   <tr>
  50. <th>##</th>
  51. <th>Name</th>
  52. <th>code</th>
  53. <th>unit</th>
  54. <th>rate</th>
  55.   </tr>
  56. </thead>
  57. <tbody>
  58. </tbody>
  59.   </table>
  60.   
  61. </body>

  62. <script>
  63. $(document).ready(function(){
  64. $('#myTable').focus();
  65. });

  66. function highlight(tableIndex) {
  67. if ((tableIndex + 1) > $('#myTable tbody tr').length) //restart process
  68. tableIndex = 0;

  69. if ($('#myTable tbody tr:eq(' + tableIndex + ')').length > 0) {
  70. $('#myTable tbody tr').removeClass('highlight');

  71. $('#myTable tbody tr:eq(' + tableIndex + ')').addClass('highlight');
  72. }
  73. }

  74. $('#goto_first').click(function() {
  75. highlight(0);
  76. });

  77. $('#goto_prev').click(function() {
  78. highlight($('#myTable tbody tr.highlight').index() - 1);
  79. });

  80. $('#goto_next').click(function() {
  81. highlight($('#myTable tbody tr.highlight').index() + 1);
  82. });

  83. $('#goto_last').click(function() {
  84. highlight($('#myTable tbody tr:last').index());
  85. });

  86. $(document).keydown(function(e) {

  87. switch (e.which) {
  88. case 38:
  89. $('#goto_prev').trigger('click');
  90. break;
  91. case 40:
  92. $('#goto_next').trigger('click');
  93. break;
  94. }

  95. });

  96. $(document).ready(function() {
  97.   var items = [];

  98.   $("#myTable tr").on('click', function(e) {

  99. var newTr = $(this).closest("tr").clone();
  100. items.push(newTr);
  101. newTr.appendTo($("#cloneTable"));
  102.   });
  103. })
  104. </script>
  105. </html>