hi
my code is using javascript to dynamically build an html table and fill the table with html buttons.
my code:
<html>
<head>
<title>javascript1</title>
</head>
<script lnaguage="javascript">
function Init()
{
var table = document.createElement("tbody");
var newrow,newcol,tmp;
var array = new Array(16);
for (var i=0 ; i<16 ; i++)
array[i] = 0;
for (var row = 0 ; row <4 ; row++)
{
newrow = document.createElement("tr");
for (var col = 0 ; col<4 ; col++)
{
newcol = document.createElement("td");
var button = document.createElement('input');
button.type = 'button';
button.width = '60';
button.height = '60';
button.id = (row*10 + col).toString();
newcol.appendChild(button);
newrow.appendChild(newcol);
if (row == 3 && col == 3)
break;
while (array[tmp = Math.floor((15)*Math.random()) + 1] == 1);
array[tmp]++;
button.value = tmp;
}
table.appendChild(newrow);
}
gameTable.appendChild(table);
}
function onClick(bid)
{
}
window.onload=Init;
</script>
<body>
<form id="myForm">
<div style=" color:Blue; font-size:large; font-style:oblique; left:500; top:30; position:absolute">
Fifteen Javascript
</div>
<div style="left:450; top:150; position:absolute"">
<table id="gameTable"></table>
</div>
</form>
</body>
</html>
my question is how can i add an eventhandler for a button click for every button.
i want to add an onClick javascript function to be performed on every button click and send the buttons id as parameter.
i tried to add this line inside the loop:
newcol.children[0].onclick = onClick(newcol.children[0].id);
but it doesn't seems to work.