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.

3
  • You already have the tr object. Have you tried applying insertCell into that? Oct 18, 2017 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? Oct 18, 2017 at 21:16
  • I am not sure.. you can probably use the browser developer tools and debug the javascript Oct 18, 2017 at 21:23

1 Answer 1

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

2
  • I have a question. In this test case did it add 3 cells with New Cell as the value? Oct 19, 2017 at 13:31
  • Add the result above.
    – Lee
    Oct 20, 2017 at 3:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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