简体   繁体   中英

Unable to get value of dynamically generated cell using document.getElementById

I am adding rows to an HTML table which already has 2 rows, using below javascript code:

<table id="dataTable" class="CSSTableGenerator">
        <tbody>
        <tr>
                <td><label>No.of internal corners</label></td>
                <td><input type="text" id="intCorner" class="corner"></td>
                <td><label>No.of external corners</label></td>
                <td><input type="text" id="extCorner" class="corner"></td>
        </tr>
        </tbody>
            <tr>
                <td><label>Wall 1</label></td>
                <td><label>Wall 2</label></td>
                <td><label>Wall 3</label></td>
                <td><label>Wall 4</label></td>
            </tr>
            <tr>
                <td><input type="text" id="wall00" class="wall"></td>
                <td><input type="text" id="wall01" class="wall"></td>
                <td><input type="text" id="wall02" class="wall"></td>
                <td><input type="text" id="wall03" class="wall"></td>
            </tr>
        </tbody>
    </table>
<input type="button" value="Add more walls" onclick="addRow('dataTable')">
function addRow(tableID) {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var cell0 = row.insertCell(0);
        var element0 = document.createElement("input");
        element0.type = "text";
        element0.name = "txtbox[]";
        element0.className ="wall";
        element0.id="wall"+rowCount-2+"0";
        cell0.appendChild(element0);
        var cell1 = row.insertCell(1);
        var element1 = document.createElement("input");
        element1.type = "text";
        element1.name = "txtbox[]";
        element1.className ="wall";
        element1.id="wall"+rowCount-2+"1";
        cell1.appendChild(element1);
        var cell2 = row.insertCell(2);
        var element2 = document.createElement("input");
        element2.type = "text";
        element2.name = "txtbox[]";
        element2.className ="wall";
        element2.id="wall"+rowCount-2+"2";
        cell2.appendChild(element2);
        var cell3 = row.insertCell(3);
        var element3 = document.createElement("input");
        element3.type = "text";
        element3.name = "txtbox[]";
        element3.className ="wall";
        element3.id="wall"+rowCount-2+"3";
        cell3.appendChild(element3);
    }

But when I use below code to get value of dynamically added cells it says that document.getElementById() is null inside for loop. It works for cells added by HTML code.

function sumOppositeWalls(){
        var table = document.getElementById('dataTable');
        var rowCount = table.rows.length;
        alert(rowCount);
        var minusCount=0;
        for(var i=0;i<rowCount-2;i++){
            var wall1=parseFloat(document.getElementById('wall'+i+'0').value);
            var wall2=parseFloat(document.getElementById('wall'+i+'1').value);
}

Please suggest any solution or let me know if I am doing some mistake.

The problem is how is are creating the id for the dynamic elements.

You are using numeric subtraction along with string concatenation like element2.id="wall"+rowCount-2+"2"; which is wrong.

You need to separate the numerical computation out as a separate unit, else "wall"+rowCount will get processed with will give wall2 then 'wall2' - 1 will be NaN since one operand is not a number so the end result will be something like NaN2

element2.id="wall"+(rowCount-2)+"2"; 

Since you are using the rowCount-2 value in multiple places, use a variable

var idCounter = rowCount - 2;

then

element0.id = "wall" + idCounter + "0";

Demo: Fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM