javascript - Adding new row to HTML table dynamically with JScript -
i writing code in html + jquery , need add rows table dynamically.
i've written code add rows dynamically, doesn't seem work.
here jscript code :
<script language="javascript"> function addrow(tableid) { var table = document.getelementbyid(tableid); var rowcount = table.rows.length; var row = table.insertrow(rowcount); var cell1 = row.insertcell(0); var element1 = document.createelement("input"); element1.type = "date"; element1.name="datebox[]; element1.id = "dt"; cell1.appendchild(element1); var cell2 = row.insertcell(1); cell2.innerhtml = rowcount + 1; var element2 = document.createelement("input"); element2.type = "select"; element2.name = "selectbox[]"; element2.id = "slct"; cell2.appendchild(element2); var cell3 = row.insertcell(2); cell3.innerhtml = rowcount + 1; var element3 = document.createelement("input"); element3.type = "text"; element3.name = "txtbox[]"; element3.id = "txt"; cell3.appendchild(element3); table.appendchild(cell1); } </script>
this table:
<table id = "tab" style="width:500px"> <tr> <th>date</th> <th>treatment goal</th> <th>comment</th> </tr> <tr> <td><input type="date" name="datebox[]" id = "dt"/></td> <td> <select name="selectbox[]" id = "slct"> </select> </td> <td><input type="text" name="txtbox[]" id = "txt"/></td> </tr> </table>
i calling function in onclick event of button :
<input type="button" id="add" value="+" name="add" onclick="addrow('tab')"/>
the html page doesn't respond click event , nothing happens. cannot understand what's going wrong.
the first problem syntax error on line (you didn't close double quote):
element1.name="datebox[]; ^ missing "
the second problem appending cell table wrong, should appending row:
table.appendchild(row); ^ changed row cell
Comments
Post a Comment