Add <tr> dynamically

Add <tr> dynamically

Hi,

I have following code. I want to have a button at top so when it is clicked a new row added under current containing all contents but ID's for row, select and text boxes should increment by 1.

Here are two jquery scripts - dynamic select box and clone row.
Each of them work fine but I cannot find the way to use it together in one script. or if someone suggest another short code for that.

Thank you for any suggestion!

           
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns= "http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
  5.  
  6.  
  7.  
  8. <script type= "text/javascript">
  9. $ ( function ( ) {
  10. // start a counter for new row IDs
  11. // by setting it to the number
  12. // of existing rows
  13. var newRowNum = 0;
  14.  
  15. // bind a click event to the "Add" link
  16. $ ( '#addnew' ). click ( function ( ) {
  17. // increment the counter
  18. newRowNum += 1;
  19.  
  20. // get the entire "Add" row --
  21. // "this" refers to the clicked element
  22. // and "parent" moves the selection up
  23. // to the parent node in the DOM
  24. var addRow = $ ( this ). parent ( ). parent ( );
  25.  
  26. // copy the entire row from the DOM
  27. // with "clone"
  28. var newRow = addRow. clone ( );
  29.  
  30. // set the values of the inputs
  31. // in the "Add" row to empty strings
  32. $ ( 'input', addRow ). val ( '' );
  33.  
  34. // replace the HTML for the "Add" link
  35. // with the new row number
  36. $ ( 'td:first-child', newRow ). html (newRowNum );
  37.  
  38. // insert a remove link in the last cell
  39. $ ( 'td:last-child', newRow ). html ( '<a href="" class="remove">Remove<\/a>' );
  40.  
  41. // loop through the inputs in the new row
  42. // and update the ID and name attributes
  43. $ ( 'input', newRow ). each ( function (i ) {
  44. var newID = newRowNum + '_' + i;
  45. $ ( this ). attr ( 'id',newID ). attr ( 'name',newID );
  46. } );
  47.  
  48. // insert the new row into the table
  49. // "before" the Add row
  50. addRow. before (newRow );
  51.  
  52. // add the remove function to the new row
  53. $ ( 'a.remove', newRow ). click ( function ( ) {
  54. $ ( this ). parent ( ). parent ( ). remove ( );
  55. return false;
  56. } );
  57.  
  58. // prevent the default click
  59. return false;
  60. } );
  61. } );
  62. </script>
  63.  
  64.  
  65. <script language= "javascript">
  66. function makeSublist (parent,child,isSubselectOptional,childVal )
  67. {
  68. $ ( "body" ). append ( "<select style='display:none' id='"+parent+child+ "'></select>" );
  69. $ ( '#'+parent+child ). html ($ ( "#"+child+ " option" ) );
  70.  
  71. var parentValue = $ ( '#'+parent ). attr ( 'value' );
  72. $ ( '#'+child ). html ($ ( "#"+parent+child+ " .sub_"+parentValue ). clone ( ) );
  73.  
  74. childVal = ( typeof childVal == "undefined" )? "" : childVal ;
  75. $ ( "#"+child ). val (childVal ). attr ( 'selected', 'selected' );
  76.  
  77. $ ( '#'+parent ). change ( function ( ) {
  78. var parentValue = $ ( '#'+parent ). attr ( 'value' );
  79. $ ( '#'+child ). html ($ ( "#"+parent+child+ " .sub_"+parentValue ). clone ( ) );
  80. if (isSubselectOptional ) $ ( '#'+child ). prepend ( "<option value='none' selected='selected'> -- Select -- </option>" );
  81. $ ( '#'+child ). trigger ( "change" );
  82. $ ( '#'+child ). focus ( );
  83. } );
  84. }
  85.  
  86. $ (document ). ready ( function ( )
  87. {
  88. makeSublist ( 'child', 'grandsun', true, '' );
  89. makeSublist ( 'parent', 'child', false, '1' );
  90.  
  91.  
  92.  
  93. $ ( "#selectListButton1" ). click ( function ( ) {
  94. alert ( 'Value is: ' + $ ( '#parent' ). val ( ) );
  95. } );
  96. $ ( "#selectListButton2" ). click ( function ( ) {
  97. alert ( 'Text is: ' + $ ( '#child :selected' ). text ( ) );
  98. } );
  99.  
  100.  
  101.  
  102.  
  103.  
  104. } );
  105. </script>
  106. </head>
  107. <body>
  108.  
  109.  
  110.  
  111.  
  112. <form method= 'POST' name= 'signupForm' class= 'cmxform' id= 'signupForm' action= '#'>
  113. <table cellspacing= '0'>
  114. <tr></tr>
  115. <tr>
  116. <td><a id= "addnew" href= "">Add< /a></td>
  117. <td>
  118. <select id= "parent">
  119. <option value= "1">Flower</option>
  120. <option value= "2">Animal</option>
  121.  
  122. </select>
  123. </td>
  124.  
  125. <td>
  126. <select id= "child">
  127. <option class= "sub_1" value= "1">Rose</option>
  128. <option class= "sub_1" value= "2">Sunflower</option>
  129. <option class= "sub_1" value= "3">Orchid</option>
  130. <option class= "sub_2" value= "4">Cow</option>
  131. <option class= "sub_2" value= "5">Dog</option>
  132.  
  133. <option class= "sub_2" value= "6">Cat</option>
  134. <option class= "sub_2" value= "7">Tiger</option>
  135. </select>
  136. </td>
  137. <td>
  138. <select id= "grandsun">
  139. <option class= "sub_1" value= "1">Rose type 1</option>
  140. <option class= "sub_1" value= "2">Rose type 2</option>
  141. <option class= "sub_1" value= "3">Rose type 3</option>
  142.  
  143. <option class= "sub_2" value= "4">Sunflower type 1</option>
  144. <option class= "sub_2" value= "5">Sunflower type 2</option>
  145. <option class= "sub_3" value= "6">Orchid type 1</option>
  146. <option class= "sub_3" value= "7">Orchid type 2</option>
  147. <option class= "sub_4" value= "8">Cow type 1</option>
  148. <option class= "sub_4" value= "9">Cow type 2</option>
  149.  
  150. <option class= "sub_5" value= "10">Dog type 1</option>
  151. <option class= "sub_6" value= "11">Cat type 2</option>
  152. <option class= "sub_7" value= "12">Tiger type 2</option>
  153. <option class= "sub_7" value= "13">Tiger type 2</option>
  154. <option class= "sub_7" value= "14">Tiger type 3</option>
  155.  
  156. </select>
  157. </td>
  158. </tr>
  159. <tr>
  160. <td>
  161. <button id= "selectListButton1">Get Value</button> <button id= "selectListButton2">Get Text</button>
  162. </td>
  163.  
  164. </tr>
  165. </table>
  166. </form>
  167.  
  168. </body>
  169. </html>


Regards,