JQuery: How to add an existing <td> element using its id to a new <tr> element?

Viewed 299

The relevant html for this is:

<table class="table" id="constraintTable">
    <tr>
        {% for header in headings %}
        <th>{{ header }}</th>
        {% endfor %}
        <th>Action</th>
    </tr>
    {% for row in data %}
    <tr>
        {% for cell in row%}
        <td>{{ cell }}</td>
        {% endfor %}
        <td id="editDelete">
            <a href="#" id="editRow">Edit</a>
            <a href="#" id="deleteRow">Delete</a>
        </td>
    </tr>
    {% endfor %}
</table>
<button type="button" class="btn btn-primary" id="addConstraint" onclick="constraintAddToTable();>Add New Constraint</button>

Now Iam trying to add a row to this table using the following Jquery: (assuming the 'row' collection in the html above has only 2 entries)

function constraintAddToTable() {
    if ($("#constraintTable tbody").length == 0) {
        $("#constraintTable").append("<tbody></tbody>");
    }
    
    $("#constraintTable tbody").append("<tr>" +
        "<td>" + "dataval1" + "</td>" +
        "<td>" + "dataval2" + "</td>" +
        $("#editDelete") +  
        "</tr>");
}

But this does not work. Only dataval1 and dataval2 gets in and the last column is just blank. I even tried adding the row without the edit/delete column and later adding the cloumn like this:

$("#constraintTable tr: last").append($("#editDelete"))

But this doesn't work as well. Any idea why this doesn't work and what needs to be done instead?

3 Answers

Looking at your HTML code, you don't have tbody tag.

Edit your HTML code as below and try.

<table class="table" id="constraintTable">
    <tr>
        {% for header in headings %}
        <th>{{ header }}</th>
        {% endfor %}
        <th>Action</th>
    </tr>
    <tbody>          //tbody starts
    {% for row in data %}
    <tr>
        {% for cell in row%}
        <td>{{ cell }}</td>
        {% endfor %}
        <td id="editDelete">
            <a href="#" id="editRow">Edit</a>
            <a href="#" id="deleteRow">Delete</a>
        </td>
    </tr>
    {% endfor %}
    </tbody>         //tbody ends
</table>

You need to first create a copy (clone) and then append. Try this:

`$("#constraintTable tr: last").append($("#editDelete").clone(true))`

You cannot use same id for mutliple element instead use class .Then , whenever your function gets called you can clone your td.editDelete and add same inside your generated html using $(htmls).html().

Demo Code :

function constraintAddToTable() {
  if ($("#constraintTable tbody").length == 0) {
    $("#constraintTable").append("<tbody></tbody>");
  }
  //get td >first clone it
  var htmls = $("td.editDelete:first").clone()
  //add inside your genearted htmls...
  $("#constraintTable tbody").append("<tr>" +
    "<td>" + "dataval1" + "</td>" +
    "<td>" + "dataval2" + "</td><td>" +
    $(htmls).html() +
    "</td></tr>");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table" id="constraintTable">
  <tr>
    <th>Soemthing</th>
    <th>Soemthing1</th>
    <th>Action</th>
  </tr>
  <tr>
    <td>1</td>
    <td>2</td>
    <!--use class-->
    <td class="editDelete">
      <a href="#" class="editRow">Edit</a>
      <a href="#" class="deleteRow">Delete</a>
    </td>
  </tr>
  <tr>
    <td>11</td>
    <td>22</td>
    <td class="editDelete">
      <a href="#" class="editRow">Edit</a>
      <a href="#" class="deleteRow">Delete</a>
    </td>
  </tr>
</table>
<button type="button" class="btn btn-primary" id="addConstraint" onclick="constraintAddToTable();">Add New Constraint</button>

Related