"Dynamic Table row creation and Deletion"
Bootstrap 3.1.0 Snippet by jfenn

<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script> <script src="//code.jquery.com/jquery-1.11.1.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-bordered table-hover" id="tab_logic"> <thead> <tr > <th class="text-center"> Leg </th> <th class="text-center"> Origin </th> <th class="text-center"> Departure Time </th> <th class="text-center"> Destination </th> <th>Arrival Time</th> </tr> </thead> <tbody> <tr id='addr0'> <td> 1 </td> <td> <input type="text" name='origin0' placeholder='CYQT' class="form-control"/> </td> <td> <input type="text" name='departure0' placeholder='2014-06-01 13:45' class="form-control"/> </td> <td> <input type="text" name='destination0' placeholder='CYEL' class="form-control"/> </td> <td> <input type='text' name='arrival0' placeholder='2014-06-01 15:15' class="form-control" /> </td> </tr> <tr id='addr1'></tr> </tbody> </table> </div> </div> <a href="#" class="btn btn-primary btn-success"><span class="glyphicon glyphicon-plus"></span> Add Leg</a> <a id="add_row" class="btn btn-default pull-left">Add Row</a><a id='delete_row' class="pull-right btn btn-default">Delete Row</a> </div>
$(document).ready(function(){ var i=1; $("#add_row").click(function(){ $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input name='origin"+i+"' type='text' placeholder='Name' class='form-control input-md' /> </td><td><input name='departure"+i+"' type='text' placeholder='Mail' class='form-control input-md'></td><td><input name='destination"+i+"' type='text' placeholder='Mobile' class='form-control input-md'></td><td><input name='arrival"+i+"' type='text' placeholder='2014-mm-dd hh:mm' class='form-control input-md'/></td>"); $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>'); i++; }); $("#delete_row").click(function(){ if(i>1){ $("#addr"+(i-1)).html(''); i--; } }); });

Related: See More


Questions / Comments: