Baran Topal

Baran Topal


May 2024
M T W T F S S
« Feb    
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


simple table in HTML and default value should come via array content from JS

baranbaran

Long ago, I needed to populate a table content and in the table there shall be textboxes created on the fly. I wanted default values in my array to be appearing in my textboxes by default.

Here is the old javascript way within HTML.

<html>
 <head>
 <script language=javascript>
 var myCars=new Array("Saab","Volvo","BMW");
 function loadData()
 {
 var t=document.getElementById("t1");
 for (var i in myCars)
 {
 // Insert a row in the table at the last row
 var newRow = t.insertRow(t.rows.length);
 // Insert a cell in the row at index 0
 var newCell = newRow.insertCell(0);
 var textBox=document.createElement("input");
 textBox.type="text";
 textBox.value=myCars[i];
 newCell.appendChild(textBox);
 }
 }
 </script>
 </head>
 <body onload="loadData()">
 <table id="t1" border=1>
 </table>
 </body>
</html>