Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Dynamic table create?

Tags:

html

jquery

How to create Dynamic table using JQuery I created with .append but problem is when i am clicking on again so create anther table but i want add in to a same table so how can i do that please help me.

$(function () {
    $("#addProduct").click(function () {
        var table = $('<table></table>').addClass('foo');
        for (i = 0; i < 10; i++) {
            var row = $('<tr></tr>');
            for (i = 0; i < 10; i++) {
                var row1 = $('<td></td>').addClass('bar').text('result ' + i);
                table.append(row);
                row.append(row1);
            }
        }
        $('#someContainer').append(table);
    });
});

This is HTML

<button id="addProduct">Add Product</button>

<div id="someContainer"></div>
like image 961
user2424382 Avatar asked Sep 19 '26 05:09

user2424382


1 Answers

Try my answer

$(function () {
    $("#addProduct").click(function () {
          var table = $('<table></table>').addClass('foo');
        for (var i = 0; i < 10; i++) {
                row = $('<tr></tr>');
                for (var j = 0; j < 10; j++) {
                    var rowData = $('<td></td>').addClass('bar').text('result ' + j);
                    row.append(rowData);
                }
                table.append(row);
            }

        if ($('table').length) {
             $("#someContainer tr:first").after(row);
        }
        else {
            $('#someContainer').append(table);
        }
    });
});
like image 167
Olrac Avatar answered Sep 20 '26 21:09

Olrac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!