On 7 Aug., 12:13, "Michael Munch" <watch4mu...@gm ail.comwrote:
<html>
What about a DOCTYPE?
*<head>
*<script language="JavaS cript" type="text/javascript">
language="JavaS cript" is deprecated and can be dropped.
Add a global variable to store the reference to the input field:
var myNameField;
*function Test_Build() {
* var tbody =
document.getEle mentById('tblli st').getElement sByTagName("TBO DY")
* [0];
Tables have a collection "tBodies", which is preferable to using
getElementsByTa gName. Also you should make sure getElementById
actually found something:
var table = document.getEle mentById('tblli st');
if (table) {
var tbody = table.tBodies[0];
// ...
}
* var row = document.create Element("TR");
* var td = document.create Element("TD");
Consider using the methods insertRow() and insertCell(). That way you
don't need to append them
var row = tbody.insertRow (-1);
var td = row.insertCell(-1);
* td.appendChild( document.create TextNode(""));
* S = document.create Element("input" );
* S.setAttribute( "type","tex t");
* S.setAttribute( "name","txtName ");
* S.setAttribute( "value","Michae l")
Use the previously declared global variable here and avoid
setAttribute since it is broken in IE 6:
myNameField = document.create Element("input" );
myNameField.typ e = "text";
myNameField.nam e = "txtName";
myNameField.val ue = "Michael";
* td.appendChild( S);
td.appendChild( myNameField);
The following appendChilds are unnecessary when using insertRow/Cell.
* row.appendChild (td);
* tbody.appendChi ld(row); }
*function Test_Read() {
// * alert(document. getElementById( 'txtName'));
// * alert(document. getElementByTag Name('txtName') .value);
* *}
You can't use getElementById nor getElementByTag Name because "txtName"
is its *name* and neither its *id* nor its *tag name*.
Instead you use the reference to field from the global variable here
now:
alert(myNameFie ld.value);
If you don't use the reference variable. you can use normal form
element reference (if you give the form a name and not an ID):
alert(document. forms["FormName"].elements["txtName"].value);
*</script>
*</head>
*<body>
* <form id="FormName">
Give the form a name if you need to reference it or its fields:
<form name="FormName" >
Don't confuse id and name. They are two distinct attributes.
Unfortunately IE does consider them the same, so don't you let it
confuse you.
* <input type="button" value="Create a textfield" onClick="Test_B uild();">
* <input type="button" value="Read value from textfield"
onClick="Test_R ead();">
* <table id="tbllist" cellspacing="0" cellpadding="0" border="0"
width="340">
* *<tbody>
* *<tr>
* * <td>List of name</td>
* *</tr>
* *</tbody>
* </table>
* </form>
*</body>
</html>
If you want to add more that one field, you'll need to store the
references some other way, for example in an array.
Robin