How to remove dynamically added element

How to remove dynamically added element

Hey Guys,

Im extremely new to jquery and im trying to create an invoice system for personal use. Thus far Ive researched online and I got this far, but I cant figure out how to remove a dynamically added element after its been created.

My code:

  1. <script>
  2. $(document).ready(function(){
  3.     
  4.     var table = $("#TextBoxesGroup");
  5.     var row = table.find("tr").eq(1);
  6.     var count = 0;
  7.     
  8.     $("#addButton").click(function(e){
  9.         if (table.find("tr").length >= 51){
  10.             alert("Maximum of 50 rows");
  11.             return;
  12.         }
  13.         var newRow = row.clone();
  14.         var regex = new RegExp("data\[[0-9]+\]", "g");
  15.         newRow.html(newRow.html().replace(regex, "data[" + (++count) + "]"));
  16.         table.append(newRow);
  17.     });
  18. $("#rmvButton").click(function(e){
  19.         $("#remove").remove();
  20.     });
  21.     
  22.     $(document).on('keyup', "*[data-field='quantity'],*[data-field='price']", function(e){
  23.         var thisRow = $(this).parents("tr:first");
  24.         var rowTotalField = thisRow.find("*[data-field='total']");
  25.         var price = parseFloat(thisRow.find("*[data-field='price']").val());
  26.         var quantity = parseInt(thisRow.find("*[data-field='quantity']").val());
  27.         rowTotalField.val("\u00A3" + (!isNaN(price) && !isNaN(quantity) ?
  28.                        price*quantity : 0).toFixed(2));
  29.         var total = 0;
  30.         table.find("*[data-field='total']").each(function(){
  31.             var t = parseFloat($(this).val().replace("£", ""));
  32.             total += !isNaN(t) ? t : 0;
  33.         });
  34.         $("#total").text(total.toFixed(2));
  35.     });
  36.     
  37. });


  38. </script>
  39. <form action="new_invoice.php" method="post">

  40. <table id="TextBoxesGroup">
  41.    <tr>
  42.        <td colspan="4">Header row</td>
  43.     </tr>
  44.     <div id="remove">
  45.    <tr>
  46.     <td><select name="data[0][0]" class="unit">
  47.          <option value="service">Service</option>
  48.          <option value="hour">Hours</option>
  49.          <option value="days">Days</option>
  50.          <option value="product">Product</option>
  51.          </select><td>
  52.         <td><input name="data[0][1]" placeholder="Item Name" /></td>
  53.         <td><input name="data[0][2]" placeholder="Item Description" /></td>
  54.         <td><input data-field="price" name="data[0][3]" placeholder="Item Amount" class="itmamnt" /></td>
  55.         <td><input data-field="quantity" name="data[0][4]" placeholder="Item Quantity" class="itmqty" /></td>
  56.         <td><input data-field="total" name="data[0][5]" placeholder="Item Total" readonly/></td>
  57.         <td><input type="button" id="rmvButton" value=" Remove Row " /><td>
  58.     </tr>
  59.     </div>
  60. </table>
  61. <input type="button" id="addButton" value=" Add Row " />
  62. <input type="submit" value="Submit" />
  63. </form>

  64. <h2>Total Cost: $<span id="total">0.00</span></h2

Any suggestions?