Tuesday, 22 August 2017

jQuery Add Row to Table Dynamically | Add Or Append Table Row (tr) to Html Table Using jQuery

8/22/2017 - By Pranav Singh 1

In this article will show you how you can add  or append  row (tr) to table tbody using jquery. This article will cover following parts. Add Or Append Table Row (tr) to Html Table Using jQuery ,jquery add row to table dynamically, add row to table using jquery example, how to append row in table using jquery, how to add table row dynamically using jquery, jquery append row to table tbody, insert row in tbody javascript, jquery insert table row, How to add, edit and delete rows of a HTML table with jQuery.

Some of my previous articles are as follows: jQuery Ajax Duplicate Email Validation In Asp.Net MVC , Entity Framework, How to show aspx in modal pop up in asp.net, jQuery To Get Cursor Position in Contenteditable Div, How to Get Dropdown Selected Text in jQuery in Asp.net, Ajax Mvc Captcha Validation With Reload Captcha Button Using Jquery, Password Compare Validation in HTML5 Using JavaScript In Asp.Net, jQuery UI Selectable To Select By Pressing Shift and Ctrl Button, jQuery UI Resizable Div Control, jQuery UI Draggable Div Control.


So for this article we will add the jQuery library reference into the header of the page.

 
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>


Now we will prepare the html code.

  Name :  <input type="text" id="txtName" /><br /><br />
    Addr : <input type="text" id="txtAddress" /><br /><br />
    <input type="button" value="Add New Row" id="btnAddRow" />
    <br />
    <br /><br />
    <table id="commenttable" border="1" width="100%">
        <thead>
            <tr style="font-weight:bold;">
                <th>Name</th>
                <th>Address</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>

Here in above code I have taken a textbox taken two textboxes and one for address and othefor name. these two textbox values we will display in table control.

Now have a look of the below jQuery code to append the added value in table with tr.

$(document).ready(function () {
            $("#btnAddRow").click(function () {

                //Getting value from to populate
                var name = $("#txtName").val();
                var address = $("#txtAddress").val();

                //Prepare TR to add in Table
                var tr;
                tr = $('<tr/>');
                tr.append("<td>" + name + "</td>");
                tr.append("<td>" + address + "</td>");
                $('#commenttable').append(tr);

            });
        });

In above code I have read the entered values and the n taken a variable for tr and append it to the table by using append function of jquery.

Now have a look of the complete code.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>jQuery Add Row to Table Dynamically</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#btnAddRow").click(function () {

                //Getting value from to populate
                var name = $("#txtName").val();
                var address = $("#txtAddress").val();

                //Prepare TR to add in Table
                var tr;
                tr = $('<tr/>');
                tr.append("<td>" + name + "</td>");
                tr.append("<td>" + address + "</td>");
                $('#commenttable').append(tr);

            });
        });
    </script>
</head>
<body>
    Name :  <input type="text" id="txtName" /><br /><br />
    Addr : <input type="text" id="txtAddress" /><br /><br />
    <input type="button" value="Add New Row" id="btnAddRow" />
    <br />
    <br /><br />
    <table id="commenttable" border="1" width="100%">
        <thead>
            <tr style="font-weight:bold;">
                <th>Name</th>
                <th>Address</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</body>
</html>

Now we have done run the application to check the output.


Tags: ,
About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

1 comment:

  1. I want to add Delete button also here. I tried a lot of functions to delete the added row but no one is working.

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top