0
var table = document.getElementById("WrksheetTable");
for(i=0;i<arrworksheets.length;i++){
    var tr = document.createElement("tr");
    tr.id = "tr" + i;
    var td = document.createElement("td");
    td.id = "td" + i;
    var wrksheetcheckbox = document.createElement("input");
    wrksheetcheckbox.type = "checkbox";
    wrksheetcheckbox.name = arrworksheets[i];
    wrksheetcheckbox.value = arrworksheets[i];
    wrksheetcheckbox.id = arrworksheets[i];

    var wrksheetlabel = document.createElement("label");
    wrksheetlabel.htmlFor = arrworksheets[i];
    wrksheetlabel.appendChild(document.createTextNode(arrworksheets[i]));

    var brk = document.createElement("br");

    td.appendChild(wrksheetcheckbox);
    td.appendChild(wrksheetlabel);
    tr.appendChild(td);
    table.appendChild(tr);

    table.rows[i+1].insertCell(1);
}

insertCell is working in HTML but not working in Sharepoint. Am I missing something. The row has already been created though.

I am trying to insert a cell in the 2nd column and then add data to it.

  • You already have the tr object. Have you tried applying insertCell into that? – Amal Hashim Oct 18 '17 at 21:04
  • I tried tr.insertCell it didnt work. After the loop i used table.rows.length to get the number of rows. I am getting 1 (because i have one row header) even if the output should be greater than one. Are the rows not getting created? – rahul delisti Oct 18 '17 at 21:16
  • I am not sure.. you can probably use the browser developer tools and debug the javascript – Amal Hashim Oct 18 '17 at 21:23
0

When you create rows in for loop, you get a row table.rows[i+1] which doesn't exists yet, that caused the error.

Here is my test code for your reference:

<script type="text/javascript">
    window.onload = function () {
        var table = document.getElementById("WrksheetTable");
        //test data
        var arrworksheets = ["Saab", "Volvo", "BMW"];
        for (i = 0; i < arrworksheets.length; i++) {
            var tr = document.createElement("tr");
            tr.id = "tr" + i;
            var td = document.createElement("td");
            td.id = "td" + i;
            var wrksheetcheckbox = document.createElement("input");
            wrksheetcheckbox.type = "checkbox";
            wrksheetcheckbox.name = arrworksheets[i];
            wrksheetcheckbox.value = arrworksheets[i];
            wrksheetcheckbox.id = arrworksheets[i];

            var wrksheetlabel = document.createElement("label");
            wrksheetlabel.htmlFor = arrworksheets[i];
            wrksheetlabel.appendChild(document.createTextNode(arrworksheets[i]));

            var brk = document.createElement("br");

            td.appendChild(wrksheetcheckbox);
            td.appendChild(wrksheetlabel);
            tr.appendChild(td);
            table.appendChild(tr);

            var x = table.rows[i].insertCell(1);
            x.innerHTML = "New cell";
        }
    }
</script>

update: The sample result: enter image description here

  • I have a question. In this test case did it add 3 cells with New Cell as the value? – rahul delisti Oct 19 '17 at 13:31
  • Add the result above. – Lee_MSFT Oct 20 '17 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.