Click here to Skip to main content
15,886,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!

i am new in JQuery and i am trying to add dynamically table in to Html .when user click on Add Button then Jquery Automatically add 5 Column into the Row of Table .Please help me if any have Solution of this.

XML
<!Doctype HTML>
<html>
<head>
<title>Adding Table</title>
<style type="text/css">
    table
    {
        caption-side: bottom;
        display:block;
    }
</style>
</head>
<body>
<h1>Adding Dynamic Tabel</h1>
<!--<form method="Get" id="MyForm" action="#">-->
<table border="1px">
<caption>Student Management Table</caption>
<thead>
 <tr>
   <th>FirstName</th>
   <th>LastName</th>
   <th>Age</th>
   <th>Male</th>
   <th>Female</th>
 </tr>
</thead>
<tbody>
 <tr>
    <td><input type="text"></td>
    <td><input type="text"></td>
    <td><input type="text"></td>
    <td><input type="checkbox"></td>
    <td><input type="checkbox"></td>
 </tr>
</tbody>
</table>
<input type="submit" value="Add" id="Add">
</body>
<script src="jquery.js"></script>
<script type="text/javascript">
    //First Decide Form Type
    //$("form").submit(function(e)
    //{
    //  e.preventDefault();
        //Get Value form The Specific ID
        $("#Add").click(function()
        {
            //var ADD=$("#AddColumn").val();
            var Table_Row=$("<tr></tr>");
            //now Add Column into the Table
            for(var x=0;x<5;x++)
            {
                $(Table_Row[x]).append(
                               "<td><input type="text"></td>
                                <td><input type="text"></td>
                                <td><input type="text"></td>
                                <td><input type="checkbox"></td>
                                <td><input type="checkbox"></td>
                               "
                               );

            }
        });

    //});
</script>
</html>
Posted
Comments
ZurdoDev 9-Mar-15 16:23pm    
Where are you stuck?
Member 11004573 9-Mar-15 16:40pm    
When i click on button nothing is happend mean to say no column are added into the table.please tell me where is the mistake in JQuery .

1 solution

You have to assign an id to the table, then append to the tbody of this table via the id, see example:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#btnAdd").click(function(){
        $("#mytable tbody").append("<tr><td>col1</td><td>col2</td></tr>");
    });
});
</script>
</head>
<body>

<table id="mytable" border="1">
<thead>
<tr>
<th>Header 1</th><th>Header 2</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

<button id="btnAdd">Add Row...</button>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900