I got the code to generate the table with buttons i wanted,
but i also want to get a "onlick="cell_id(" + j +"," + i ")"
function within the <button>
element using js.
The j
and i
variables are to mark the row and column where the click came from and also used to create the table.
the code:
function tableCreate() {
var body = document.getElementsByTagName("body")[0];
var tbl = document.createElement("table");
var tblBody = document.createElement("tbody");
for (var j = 0; j <= 10; j++) {
var row = document.createElement("tr");
for (var i = 0; i <10; i++) {
var cell = document.createElement("td");
var button = document.createElement("button");
button.innerHTML = j +"-"+i;
cell.appendChild(button);
row.appendChild(cell);
}
tblBody.appendChild(row);
}
tbl.appendChild(tblBody);
body.appendChild(tbl);
tbl.setAttribute("border", "2");
}
function cell_id(x,y){
window.alert('x:'+x+ ' y:'+y)
}