0

I'm trying to create a dynamic table row with a click of a button. The problem is the first row gets added perfectly, but one after that starts getting nested inside the original row added.

HTML Code

<tr class="txtMult" id="addresses"></tr>

<a href="#" id="add" class="btn btn-success">+</a>

JS

<script>
  var count=2;
  $("#add").click(function(){
    var html="<td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][type]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][description]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][qty]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][unit_price]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][sub_total]' required readonly></div></td>";

    $("#addresses").append(html);
    count++;
  });
</script>

Am i doing something wrong? Or is my logic broken?

For Reference - Final Output enter image description here

2
  • 1
    You're not adding a new row. You're adding new columns to an existing row. Write the HTML for an entire row and append that to the table's body. Commented Mar 11, 2018 at 8:03
  • @Titus issue is even if i put <tr> inside the script the issue still occurs. Is there a way i can change the <tr class="txtMult" id="addresses"></tr> code without affecting the entire table? Because adding div's or span's doesn't work :S Commented Mar 11, 2018 at 8:06

3 Answers 3

1

You're appending everything to a single tr, so everything will be in one row. You need to wrap your inserted td with a tr and append to maybe a tbody.

<table>
  <thead>
    etc...
  </thead>
  <tbody id="addresses">
  </tbody>
</table>


<a href="#" id="add" class="btn btn-success">+</a>

<script>
  var count=2;
  $("#add").click(function(){
    var html="<tr><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][type]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][description]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][qty]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][unit_price]' required></div></td><td><div class='form-group'><input type='text' class='form-control' name='Jobitems["+count+"][sub_total]' required readonly></div></td></tr>";
    $("#addresses").append(html);
    count++;
  });
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

So you're suggesting have multiple <tr class="txtMult" id="addresses"></tr> tags?
Because i want the tr's to continue without creating another table here.
@KasunWijesekara TR's are just rows on the same table, you should have multiple TR;s
Thanks this method works. Needs a bit of css touch ups :) Thanks!
1

Firstly, your added element should be a tr, Secondly, you should add it to the tbody, so we select the parent of the tr:

<script>
  var count=2;
  $("#add").click(function(){
    var html="<tr>... your old tds here ...</tr>";

    $("#addresses").parents("tbody").append(html);
    count++;
  });
</script>

Comments

1

didn't get your question but I think this could be your answer

function myFunction() {
    var table = document.getElementById("myTable");
    var row = table.insertRow(0);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    cell1.innerHTML = "NEW CELL1";
    cell2.innerHTML = "NEW CELL2";
}
<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>
</head>
<body>

<p>Click the button to add a new row at the first position of the table and then add cells and content.</p>

<table id="myTable">
  <tr>
    <td>Row1 cell1</td>
    <td>Row1 cell2</td>
  </tr>
  <tr>
    <td>Row2 cell1</td>
    <td>Row2 cell2</td>
  </tr>
  <tr>
    <td>Row3 cell1</td>
    <td>Row3 cell2</td>
  </tr>
</table>
<br>

<button onclick="myFunction()">Try it</button>



</body>
</html>

2 Comments

Yea this is the way, I'll try this out and see.
Yea this is another alternative method! Thanks :) works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.