JavaScript Type Error 'childnodes' undefined

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-17 04:15:28

问题


Need to Dynamically Add/Remove rows in HTML table using JavaScript getting a type error.

Type Error: cannot read property 'childNodes' of undefined?

Catches this exception in run time during deleteRow function execution.

On pressing Add Row, a dynamic row will be created and added in the table. And on selecting the checkbox and pressing Delete Row, the row will be removed.

Following is the source.

<html>
<head>
<script>
var b = new Array();
var entryListCounter = 0;
var counter = 0;

function insertEntry(f) {

var test = 0;

    //local array collects input values
    var a = new Array();

    a[0] = f.ticker.value;
    a[1] = f.cusip.value;
    a[2] = f.quantity.value;

    //store local array in entry list array
    b[entryListCounter] = a;

    entryListCounter++;


    if (a[0] == "" && a[1] == "" || a[2] == "") {
        console.log("val not filled");
    } 
    else if(a[0].length > 0 && a[1].length > 0){
        console.log("only fill one");
    }
    else {
        var table = document.getElementById("manualEntryInputTable");
        var row = table.insertRow(table.rows.length);

        var cell1 = row.insertCell(0);
        var t1 = document.createElement("input");
        t1.id = "t" + counter;
        cell1.appendChild(t1);

        var cell2 = row.insertCell(1);
        var t2 = document.createElement("input");
        t2.id = "c" + counter;
        cell2.appendChild(t2);

        var cell3 = row.insertCell(2);
        var t3 = document.createElement("input");
        t3.id = "q" + counter;
        t3.style = "text-align:right";
        cell3.appendChild(t3);

        var cell4 = row.insertCell(3);
        var t4 = document.createElement("input");
        t4.type = "checkbox";
        t4.name = "chkbox";
        cell4.appendChild(t4);

        f.quantity.value = "";
        f.cusip.value = "";
        f.ticker.value = "";
    }
}

        function deleteRow(e) {
            try {
            var table = document.getElementById("manualEntryInputTable");
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[3].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }




</script>

</head>

<body>
<form>
        <table id="manualEntryInputTable">
            <tr>
                <td><b>T</b></td>
                <td><b>C</b></td>
                <td><b>Q</b></td>
            </tr>
            <tr>
                <td class="label"><input size="" type="text" name="ticker"
                    value="" ></td>
                <td class="label"><input size="" type="text" name="cusip"
                    value=""></td>
                <td class="label"><input size="" type="text" name="quantity"
                     style="text-align: right;" value="">
                </td>
                <td><input type="button" onClick="insertEntry(this.form)" value="Add"/>
                </td>
                <td><input type="button" onClick="deleteRow(this.form)" value="Delete"/>
                </td>
            </tr>
        </table>
</form> 

</body>
</html>

回答1:


By looking at the conditional checking for null, I would rewrite it like this:

var chkbox = row.cells[3].childNodes.length ? row.cells[3].childNodes[0] : null;

That should avoid the error being thrown, but might not get the row deleted if the cell with index 3 doesn't exist. Consider checking the value of the right cell index row.cells[YOUR_CELL_INDEX_HERE].childNodes[0]




回答2:


I am not entirely sure what you are trying to do, but the reason for exception is that you are trying to access the element which doesn't exists.

This line is giving the error row.cells[3].childNodes[0] since you do not have row.cell[3] property. The row.Cells has length 3 but since the index starts from 0, you can refer to the last element using row.cells[2] You get undefined and hence row.cells[3].childNodes[0] method doesn't work.

change it to row.cells[2].childNodes[0]

<html>
<head>
<script>
var b = new Array();
var entryListCounter = 0;
var counter = 0;

function insertEntry(f) {

var test = 0;

    //local array collects input values
    var a = new Array();

    a[0] = f.ticker.value;
    a[1] = f.cusip.value;
    a[2] = f.quantity.value;

    //store local array in entry list array
    b[entryListCounter] = a;

    entryListCounter++;


    if (a[0] == "" && a[1] == "" || a[2] == "") {
        console.log("val not filled");
    } 
    else if(a[0].length > 0 && a[1].length > 0){
        console.log("only fill one");
    }
    else {
        var table = document.getElementById("manualEntryInputTable");
        var row = table.insertRow(table.rows.length);

        var cell1 = row.insertCell(0);
        var t1 = document.createElement("input");
        t1.id = "t" + counter;
        cell1.appendChild(t1);

        var cell2 = row.insertCell(1);
        var t2 = document.createElement("input");
        t2.id = "c" + counter;
        cell2.appendChild(t2);

        var cell3 = row.insertCell(2);
        var t3 = document.createElement("input");
        t3.id = "q" + counter;
        t3.style = "text-align:right";
        cell3.appendChild(t3);

        var cell4 = row.insertCell(3);
        var t4 = document.createElement("input");
        t4.type = "checkbox";
        t4.name = "chkbox";
        cell4.appendChild(t4);

        f.quantity.value = "";
        f.cusip.value = "";
        f.ticker.value = "";
    }
}

        function deleteRow(e) {
            try {
            var table = document.getElementById("manualEntryInputTable");
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[2].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    if(rowCount <= 1) {
                        alert("Cannot delete all the rows.");
                        break;
                    }
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }




</script>

</head>

<body>
<form>
        <table id="manualEntryInputTable">
            <tr>
                <td><b>T</b></td>
                <td><b>C</b></td>
                <td><b>Q</b></td>
            </tr>
            <tr>
                <td class="label"><input size="" type="text" name="ticker"
                    value="" ></td>
                <td class="label"><input size="" type="text" name="cusip"
                    value=""></td>
                <td class="label"><input size="" type="text" name="quantity"
                     style="text-align: right;" value="">
                </td>
                <td><input type="button" onClick="insertEntry(this.form)" value="Add"/>
                </td>
                <td><input type="button" onClick="deleteRow(this.form)" value="Delete"/>
                </td>
            </tr>
        </table>
</form> 

</body>
</html>


来源:https://stackoverflow.com/questions/41966001/javascript-type-error-childnodes-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!