function addRow(a,b) { var r = document.createElement('tr'); var ca = document.createElement('td'); var cb = document.createElement('td'); var ta = document.createTextNode(a); var tb = document.createTextNode(b); var t = document.getElementById('tbl');
พยามทำความเข้าใจ แล้วประยุกต์ใช้ให้ได้นะครับ
<html>
<head>
<script language='javascript'>
function addRow(a,b) {
var r = document.createElement('tr');
var ca = document.createElement('td');
var cb = document.createElement('td');
var ta = document.createTextNode(a);
var tb = document.createTextNode(b);
var t = document.getElementById('tbl');
ca.appendChild(ta);
cb.appendChild(tb);
r.appendChild(ca);
r.appendChild(cb);
t.tBodies(0).appendChild(r);
}
</script>
</head>
<body>
<form>
Column A: <input type=text name=a>
<br>Column B: <input type=text name=b>
<br><input type=button value='add Row' onClick='javascript:addRow(a.value, b.value)'>
</form>
<table id=tbl>
<tr><td>Column A</td><td>Column B</td></tr>
</table>
</body>
</html>
// เราได้มาจากเว็บแห่งหนึ่งของคนไทยนี้หละ
<html>
<head>
<title>Test</title>
</head>
<script language="javascript">
function fncCreateElement(){
var mySpan = document.getElementById('mySpan');
var myElement1 = document.createElement('<input type="text" name="txt[]">');
myElement1.setAttribute('id',"txt1");
mySpan.appendChild(myElement1);
//*** Remove Element ***//
/*
var deleteEle = document.getElementById('txt1');
mySpan.removeChild(deleteEle);
*/
var myElement2 = document.createElement('<br>');
mySpan.appendChild(myElement2);
}
</script>
<body>
<span id="mySpan"></span>
<input name="btnButton" id="btnButton" type="button" value="Create" onClick="JavaScript:fncCreateElement();">
</body>
</html>
// อันนี้มีแบบลบได้ด้วย
<script type="text/javascript"><!--
var gFiles = 0;
function addFile() {
var li = document.createElement('li');
li.setAttribute('id', 'file-' + gFiles);
li.innerHTML = '<input type="text" name="name[]"><span onclick="removeFile(\'file-' + gFiles + '\')" style="cursor:pointer;">Remove</span>';
document.getElementById('files-root').appendChild(li);
gFiles++;
}
function removeFile(aId) {
var obj = document.getElementById(aId);
obj.parentNode.removeChild(obj);
}
--></script>
<span onclick="addFile()" style="cursor:pointer;">Add</span>
<ol id="files-root">
<li><input type="text" name="name[]">
</ol>