0

I want when i click on the +add more button it should add the element same as in line no.1 also the S.No should incremented...and each row have remove option.

code in JSFIDDLE

<a href="" id="c" class="text-right">+add more</a>

<table class="table">
    <thead>
        <tr>
            <th>S.No.</th>
            <th>Name</th>
            <th>Dose</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td style="width:80%">
                <input id="1a" type="text" class="form-control">
            </td>
            <td style="width:80%">
                <input type="number" class="form-control">
            </td>
        <tr>
    </tbody>
</table>
Dangerous
  • 4,818
  • 3
  • 33
  • 48

4 Answers4

2

You can use .clone() to copy your first tr:

$(".text-right").on("click", function() {
  var tr = $("table tr:eq(1)").clone(true);
  $(tr).find("td:first").text($("table tr").length - 1);
  $("table").append(tr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="c" class="text-right">+add more</a>
<table class="table">
  <thead>
    <tr>
      <th>S.No.</th>
      <th>Name</th>
      <th>Dose</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td style="width:80%">
        <input type="text" class="form-control">
      </td>
      <td style="width:80%">
        <input type="number" class="form-control">
      </td>
      <tr>
  </tbody>

</table>

I remove id from input cause id must be unique. Also modify a bit the code to increase the numeric value of the td correct.

Alex Char
  • 32,879
  • 9
  • 49
  • 70
2

Jquery clone() is for cloning DOM element and then you can append it. Consider code something like this:

$(function(){
    $("#c").click(function(e){
        e.preventDefault();
        $("table.table tbody tr:first").clone().appendTo("table.table tbody");
    });
})

DEMO


For increment of serial number:

$(function(){
    var counter = 1;
    $("#c").click(function(e){
        e.preventDefault();
        var tr = $("table.table tbody tr:first").clone().appendTo("table.table tbody");
        tr.find("td:eq(0)").text(++counter);
    });
}) 

See it in action

Manwal
  • 23,450
  • 12
  • 63
  • 93
0

try: html:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a href="#" id="c" class="text-right">+add more</a>
    <table class="table">
      <thead>
        <tr>
          <th>S.No.</th>
          <th>Name</th>
          <th>Dose</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td style="width:80%">
            <input type="text" class="form-control">
          </td>
          <td style="width:80%">
            <input type="number" class="form-control"/></br><a class="rm" href="#">remove</a>
          </td>
          </tr>
      </tbody>

    </table>

js:

function updateNumber() {
   x = 0
        $('table.table tbody tr').each(function(i,v){

            $(this).find('td:eq(0)').text(x+1)
            x++;
        });
}
    $("#c").on('click',function(e){
        e.preventDefault();
        var tr = $("table.table tbody tr:first").clone().appendTo("table.table tbody");
        updateNumber();
        console.log($('table.table tbody tr'));
    });
    $('body').on("click",".rm", function(e) {
        e.preventDefault();
       $(this).parents('tr').remove();
        updateNumber();
     });

jsfiddle:https://jsfiddle.net/pjeruccb/3/

madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

Have a look at this:

$(".text-right").on("click", function() {
  var prevNo = parseInt($(".table tr:last td:first-child").text());
  var tr = $("table tr:eq(1)").clone(true);
  $("table").append(tr);
  $('.table tr:last-child td:first-child').html(prevNo + 1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" id="c" class="text-right">+add more</a>
<table class="table">
  <thead>
    <tr>
      <th>S.No.</th>
      <th>Name</th>
      <th>Dose</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td style="width:80%">
        <input type="text" class="form-control">
      </td>
      <td style="width:80%">
        <input type="number" class="form-control">
      </td>
    </tr>
  </tbody>

</table>
Reema
  • 587
  • 3
  • 12
  • 37