Monday, April 22, 2019

How to add a Table row with jquery.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

<title>jQuery Add / Remove Table Rows</title>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
 
    .top {
        margin-top: 20px;
    }

</style>

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".add-row").click(function(){
            var name = $("#name").val();
            var email = $("#email").val();
            var markup = "<tr><td><input type='checkbox' name='record'></td><td>" + name + "</td><td>" + email + "</td></tr>";
            $("table tbody").append(markup);
        });
       
        // Find and remove selected table rows
        $(".delete-row").click(function(){
            $("table tbody").find('input[name="record"]').each(function(){
                if($(this).is(":checked")){
                    $(this).parents("tr").remove();
                }
            });
        });
    });   
</script>
</head>
<body>


<div class="container top">
    <div class="row">
        <div class="col-md-8 offset-md-2">

    <form>

        <!-- <input type="text" id="name" placeholder="Name">
        <input type="text" id="email" placeholder="Email Address">
        <input type="button" class="add-row" value="Add Row"> -->

    <!-- <div class="form-row"> -->
    <div class="col-md-3">
      <input type="text" class="form-control" id="name" placeholder="Name">
    </div>
    <div class="col-md-3">
      <input type="text" class="form-control" id="email" placeholder="Email">
    </div>
    <div class="col-md-2">
      <input type="button" class="form-control btn-primary add-row" value="Add Row">
    </div>
   
   
    </form>

<!--     <table>
        <thead>
            <tr>
                <th>Select</th>
                <th>Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" name="record"></td>
                <td>Peter Parker</td>
                <td>peterparker@mail.com</td>
            </tr>
        </tbody>
    </table> -->



<table class="table table-striped">
  <thead>
    <tr>
      <th>Select</th>
      <th>Name</th>
      <th>Email</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="checkbox" name="record"></td>
      <td>Otto</td>
      <td>@mdo</td>
    </tr>
     
  </tbody>
</table>

    <button type="button" class="delete-row btn">Delete Row</button>

</div>
</div>
</div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>


</body>

</html>

No comments:

Post a Comment