Jquery Dynamic Table Create?
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
Solution 1:
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);
}
});
});
Solution 2:
try this
$(function() {
$("#addProduct").click(function() {
if($('#someContainer table').length > 0)
{
var row = $('<tr></tr>');
for(i=0; i<10; i++){
var row1 = $('<td></td>').addClass('bar').text('result ' + i);
row.append(row1);
}
$('#someContainer table').append(row);
}
else
{
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);
}
});
});
live demo here.
Solution 3:
If you want to execute it for only once try using .one() in jQuery.
$(function () {
$("#addProduct").one("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);
});
});
Check JSFiddle.
Solution 4:
$(function () {
$("#addProduct").click(function () {
var table
if($('#someContainer').find("table").length > 0){
table =$('#someContainer').find("table");
}else{
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);
});
});
try this one DEMO
Solution 5:
<html><head></head><body><tableid="game_table"border="1"></table><scriptlanguage="JavaScript">
gt = document.getElementById('game_table'),
i = 0,
frag = document.createDocumentFragment(),
tr = document.createElement('tr'),
td = document.createElement('td');
while (i<10) {
var _tr = tr.cloneNode(),
j = 0;
while (j<10) {
_tr.appendChild(td.cloneNode());
var min = 1 ;
var max = 3 ;
var a = Math.floor( Math.random() * (max + 1 - min) ) + min ;
var temp = document.createElement('div');
temp.innerHTML = "str";
_tr.appendChild(temp.firstChild);
j++;
}
frag.appendChild(_tr);
i++;
}
gt.appendChild(frag);
</script></body></html>
Post a Comment for "Jquery Dynamic Table Create?"