JavaScript - How to insert text at specific index in tablecell

Asked By Saif AK on 16-Oct-18 01:20 AM
I have a table which i am dynamically generating based on user input. I am trying to insert a text into table cell on button click. Can someone suggest me how can i do this?

function CreateTable(){
    var rowCtr;
    var cellCtr;
    var rowCnt;
    var cellCnt;

    var myTableDiv = document.getElementById('myDynamicTable');

    var table = document.createElement('table');
    table.setAttribute("contenteditable", "true");
    table.border = "1";
    table.id = "myTable";


    var tableBody = document.createElement('tbody');
    table.appendChild(tableBody);

    rowCnt = document.getElementById('txtrows').value;
    cellCnt = document.getElementById('txtcols').value;

    for (rowCtr = 0; rowCtr < rowCnt; rowCtr++) {

        var tr = document.createElement('tr');
        tableBody.appendChild(tr);

        for (cellCtr = 0; cellCtr < cellCnt; cellCtr++) {

            var td = document.createElement('td');
            td.width = '120';
            td.appendChild(document.createTextNode("Row:" + rowCtr + " Column:" + cellCtr));
            tr.appendChild(td);
        }
    }
    myTableDiv.appendChild(table);
  }

Robbe Morris replied to Saif AK on 21-Oct-18 10:10 AM
Populate the cell object's .innerHTML property value.