Duplicate table row and insert new cell

I am currently attempting to duplicate a row from one table into a new table and then include an additional td at the end of the copied row containing a delete button.

Thus far, I have successfully duplicated the row but am struggling to find a method for adding the new td.

var items = [];

$('.addme').click(function() {
  var $this = $(this);
  $this.toggleClass('addme');
  if ($this.hasClass('addme')) {
    $this.val('Add').toggleClass('added');
  } else {
    $this.val('Added').toggleClass('added');
    var newTr = $(this).closest("tr").clone();
    items.push(newTr);
    newTr.appendTo($("#products_chosen"));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="products" class="table table-bordered table-striped table-shopsite display" cellspacing="0">
  <thead>
    <tr>
      <th>ID</th>
      <th>Manufacturer</th>
      <th>Name</th>
      <th>Country</th>
      <th>Price per kg.</th>
      <th>Quantity</th>
      <th>Favorite</th>
      <th></th>
    </tr>
  </thead>;

  <tr>
    <td>1726</td>
    <td>Danroots</td>
    <td>Carrot</td>
    <td>Denmark</td>
    <td>5 kr</td>
    <td><input type="number" value="" class="qty" name="qty"></input>
    </td>
    <td><i class="heart fa fa-heart-o fa-2x"></i></td>
    <td><input type="button" id="add" class="addme" Value="Add"></input>
    </td>
  </tr>>;
  <tr>
    <td>1726</td>
    <td>Danroots</td>
    <td>Carrot</td>
    <td>Germany</td>
    <td>5 kr </td>
    <td><input type="number" value="" class="qty" name="qty"></td>
    <td><i class="heart fa fa-heart-o fa-2x"></i></td>
    <td><input type="button" class="addme" Value="Add"></input>
    </td>
  </tr>;
  <tr>
    <td>1726</td>
    <td>Danroots</td>
    <td>Cucumber</td>
    <td>Spain</td>
    <td>5kr</td>
    <td><input type="number" value="" class="qty" name="qty"></td>
    <td><i class="heart fa fa-heart-o fa-2x"></i></td>
    <td><input type="button" class="addme" Value="Add"></input>
    </td>
  </tr>;

</table>;

<br>; <br>; <br>;



<div class="table">
  <table id="products_chosen" class="table table-bordered table-striped table-shopsite display" cellspacing="0">
    <thead>
      <tr class="active">
        <th>ID</th>
        <th>Manufacturer</th>
        <th>Name</th>
        <th>Country</th>
        <th>Price per kg.</th>
        <th>Quantity</th>
        <th>Favorite</th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody <tr>
      </tr>
    </tbody>
    <tfoot>
      <tr>

      </tr>
    </tfoot>

  </table>
;

Answer №1

var items = [];

$('.addme').click(function() {
  var $this = $(this);
  $this.toggleClass('addme');
  if ($this.hasClass('addme')) {
    $this.val('Tilføj').toggleClass('added');
  } else {
    $this.val('Tilføjet').toggleClass('added');
    var newTr = $(this).closest("tr").clone().append("<td><button>deletebutton</button></td>");
    items.push(newTr);
    newTr.appendTo($("#products_chosen"));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="products" class="table table-bordered table-striped table-shopsite display" cellspacing="0">
  <thead>
    <tr>
      <th>ID</th>
      <th>Producent</th>
      <th>Navn</th>
      <th>Land</th>
      <th>Pris i kr. pr. kg.</th>
      <th>Mængde</th>
      <th>Favorit</th>
      <th></th>
    </tr>
  </thead>

  <tr>
    <td>1726</td>
    <td>Danroots</td>
    <td>Gulerod</td>
    <td>Danmark</td>
    <td>5 kr</td>
    <td><input type="number" value="" class="qty" name="qty"></input>
    </td>
    <td><i class="heart fa fa-heart-o fa-2x"></i></td>
    <td><input type="button" id="add" class="addme" Value="Tilføj"></input>
    </td>
  </tr>
  <tr>
    <td>1726</td>
    <td>Danroots</td>
    <td>Gulerod</td>
    <td>Tyskland</td>
    <td>5 kr </td>
    <td><input type="number" value="" class="qty" name="qty"></td>
    <td><i class="heart fa fa-heart-o fa-2x"></i></td>
    <td><input type="button" class="addme" Value="Tilføj"></input>
    </td>
  </tr>
  <tr>
    <td>1726</td>
    <td>Danroots</td>
    <td>Agurk</td>
    <td>Spanien</td>
    <td>5kr</td>
    <td><input type="number" value="" class="qty" name="qty"></td>
    <td><i class="heart fa fa-heart-o fa-2x"></i></td>
    <td><input type="button" class="addme" Value="Tilføj"></input>
    </td>
  </tr>

</table>

<br> <br> <br>



<div class="table">
  <table id="products_chosen" class="table table-bordered table-striped table-shopsite display" cellspacing="0">
    <thead>
      <tr class="active">
        <th>ID</th>
        <th>Producent</th>
        <th>Navn</th>
        <th>Land</th>
        <th>Pris pr. kg.</th>
        <th>Mængde</th>
        <th>Favorit</th>
        <th></th>
        <th></th>
      </tr>
    </thead>
    <tbody <tr>
      </tr>
    </tbody>
    <tfoot>
      <tr>

      </tr>
    </tfoot>

  </table>

  1. Add the delete button to the cloned element by using `.append()` method

Answer №2

When this code is executed, it will change the ID of the element with the id "add" to "del": 
var items = [];

$('.addme').click(function() {
  var $this = $(this);
  $this.toggleClass('addme');
  if ($this.hasClass('addme')) {
    $this.val('Add').toggleClass('added');
  } else {
    $this.val('Added').toggleClass('added');
    var newTr = $(this).closest("tr").clone();
    newTr.find("#add").attr("id","del");
    items.push(newTr);
    newTr.appendTo($("#products_chosen"));
  }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to add shadow effects to a DIV using CSS?

Is there a way to add shadows within an element using CSS? I attempted the following method but it did not work: #element { shadow: 10px black; } ...

Is there a way to use HTML and JS to draw custom lines connecting elements?

Sorry if this question has been asked before. Is there a way to create straight lines connecting HTML elements with just HTML and JavaScript, without relying on SVG or Canvas? If so, what would be the most effective approach? ...

Experiencing difficulties with using the javascript alternative to jQuery.ajax()

Here is a JavaScript Object: const data = { name: "John", age: 30, } const jsonData = JSON.stringify(data); The AJAX Request below successfully passes the data: $(function(){ $.ajax({ url:'two.php', ty ...

Adjust the TextArea content according to the quantity of selections made in the listbox

If I have a listbox alongside a textarea: <textarea id="MyTextArea"></textarea> <select id="SelectList" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="me ...

Yii2 Gridview - Horizontal Scroll Bar at the Top of the Page

I found a helpful post on Stack Overflow about creating a Yii2 Gridview with a fixed first column (Yii2 : Gridview with fixed first column), and now I'm looking to add a horizontal scrollbar at the top of the grid. My users really appreciate being ab ...

Having trouble retrieving input data in XML format, resulting in a TypeError: 'NoneType' object does not support item assignment

Can you please help me figure out what I did wrong? I managed to create data successfully in JavaScript, but whenever I try in Python, I keep encountering this error: getVals = list([val for val in partner_name[:25] if val.isalnum()]) #limit the domai ...

The footer has a wandering nature, constantly moving around as the wrapper mysteriously vanishes

I understand that this question has been asked multiple times before, but I am struggling to get it right! My goal is to keep the footer at the bottom of the page, but it seems to be acting strangely. Here is my HTML: <!DOCTYPE HTML> <html& ...

Is there a way to retrieve the filename of a file uploaded using a shiny fileInput function?

This shiny app has a feature where users land on the 'Upload data' panel upon launching. To restrict access to the other two 'tabpanels', users must first upload both necessary files in the 'Upload data' tab. The condition for ...

Adding Numerous Elements to a Container

In my HTML code, there is a div element with the id of controls. I am attempting to add a div containing 2 input fields using this method: this.controls = controlBtns .append('div') .classed({'time-container': true}) .appe ...

Is it possible to use a single validation rule for multiple fields in JQuery?

I have a question about JQuery validation and custom rules. I am new to this so please bear with me :) When using JQuery validation, if I want a textbox to be required, I can simply add the css class "required" to the element and it will be validated acco ...

Tips for eliminating the white space bordering an image within a Bootstrap card

<div class="container"> <h1 class="text-center">Adding Items to Cart Using PHP</h1> <div class="row"> <?php $sql="select * from products"; $res=$con->query($sql) ...

Is there a more effective method for positioning items in a navigation bar other than using padding-left?

Hey there, I've been working on this navigation bar for quite some time now and finally found a way to center the menu items. However, I want to make sure I'm doing it the right way. Does anyone have suggestions on how to center all the "li" elem ...

Performing two consecutive nested AJAX requests in jQuery using JavaScript

I am facing an issue with my code. My goal is to create a cryptocurrency ranking feature on my website. I have integrated an API that provides logos, symbols, indices, prices, etc. of cryptocurrencies. However, I encountered a problem as this API doesn&apo ...

The Angular Table row mysteriously vanishes once it has been edited

Utilizing ng-repeat within a table to dynamically generate content brings about the option to interact with and manage the table contents such as edit and delete. A challenge arises when editing and saving a row causes it to disappear. Attempts were made ...

Looking for insights on reading and presenting XML files from a URL? Learn how to achieve this effortlessly with the power

Currently, I'm in the process of developing an innovative Android application using PhoneGap. My main objective is to dynamically present the data stored within a customized XML file hosted on my website that follows the .php format. Do you happen to ...

ng-click-outside event triggers when clicking outside, including within child elements

I am looking to trigger a specific action when I click outside of the container. To achieve this, I have utilized the ng-click-outside directive which works well in most cases. However, there is one scenario where an issue arises. Inside the container, the ...

A guide to eliminating duplicate values within an array and across multiple arrays with the help of jQuery

Looking for assistance with jQuery to find and alert repeated values in my code. Below are example arrays: a1 = {1,2,3,4,5,6,4} a2= {9,8,7}, a3= {a,b,c,d,e,7} I want to identify the value 4 in array "a1" and give an alert, as well as identify the value ...

Touchwipe incorporation - single-page website script

Today has been dedicated to troubleshooting and searching for a solution regarding the integration of TouchWipe () on a custom one-page-site script based on Parallax that I found perfect for my latest project (). The script itself performs beautifully wit ...

Progress Bar Modules

I am currently working on creating a customizable animated progress bar that can be utilized as follows: <bar [type]="'health'" [percentage]="'80'"></bar> It is functional up to the point where I need to adjust different p ...

Is there a way to retrieve the central anchor point position (x, y) of the user's selection in relation to the document or window?

Is there a way to retrieve the center anchor point position (x, y) of the user's selection relative to the document or window? I have tried using window.getSelection() to get selected nodes, but I am unsure how to obtain their position: See an examp ...