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)
}
addEventListener().button.addEventListener('onclick')track the button sense it has no id or anything to id it form the rest of the buttonscellIndexon table cell elements androwIndexon table row elements exist. So this whole thing can be done by adding one click handler to the table, checking if the event target was abutton, and if so going up to the parent cell and row, and getting the respective index values from there - jsfiddle.net/7vdLekz2