1

This is example of my code.. from the example, I've got two rows.. What I want to do is, I want to add the two rows dynamically.. thank you

<table id="datatable">
    <tr>
        <td><input type="text" /></td>
    </tr>
    <tr>
        <td><input type="text" /></td>
    </tr>
</table> 
<button type="button" id="add" onclick="addRow('dataTable')">
    <b>Add</b>
</button>
function addRow(tableID) {
    alert('ttt');
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for (var i = 0; i < colCount; i++) {
        var newcell = row.insertCell(i);
        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        //alert(newcell.childNodes);
        switch(newcell.childNodes[0].type) {
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "checkbox":
                newcell.childNodes[0].checked = false;
                break;
            case "select-one":
                newcell.childNodes[0].selectedIndex = 0;
                break;
        }
    }
}

jsfiddle example

3
  • yes can.. just give your best answer :) Commented Feb 26, 2013 at 10:58
  • see: stackoverflow.com/questions/10486108/… Commented Feb 26, 2013 at 11:03
  • @AlenGenzić : the example didnt work with me.. could you please modify my script so i can use it? tq Commented Feb 26, 2013 at 13:38

3 Answers 3

2

Try this:

HTML

<table id="datatable">
       <tr>
        <td><input type="text"  /></td>
       </tr>
       <tr>
        <td><input type="text"  /></td>
       </tr>
     </table> 

<button type="button" id="add" onclick="addRow('dataTable')">
        <b>Add</b>
 </button>

JS

 $(document).ready(function() {
    $('#add').click(function(){ 
        $('#datatable').append(' <tr> <td>Account No</td> <td>:</td> <td colspan="2"><input size="20" type="text" /></td> <td>Bill No</td> <td>:</td> <td><input type="text" /></td> </tr> <tr> <td>Name</td> <td>:</td> <td colspan="2"><input type="text" /></td> <td>Bill date</td> <td>:</td> <td><input type="text" /></td> </tr>');
}); 
});

Fiddle: http://jsfiddle.net/zLXmQ/4/

Sign up to request clarification or add additional context in comments.

12 Comments

your jsfiddle example is mine :)
obviously...I had to keep it same as you but working so that u can paste it as it is
@SyazriAris Did u used my answer??
yes.. i try use your answer.. but its not what i want.. can u try modify my answer so it usable? thanks
A row is adding on click of add. What more may i add in it to make it useful for u?
|
0

Just browsing through and would like to point out that your table id- is datatable and your parameter is dataTable. hopefully thats pertinent

Comments

0

simply you can do this :

function addRow(tableId)
{
  $('#' + tableId + ' tr:last').after('<tr><td>Enter You New Row Data Here..</td></tr>');
}

for more information check this : Add table row in jQuery

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.