Search code examples
javascriptasp-classicremovechild

NotFoundError: DOM Exception 8


straight to the point:

function dell(a) {
    var id = a.split('');
    var rs_array = ["nr", "ps", "aan", "des", "subs", "del"];
    var r_array = ["artnr", "ps", "aan", "des", "subtots", "del"];
    for (i = 0; i < 6; i++) //ligt aan nummer
    {
        var regels = document.getElementById(rs_array[i]);
        regels.removeChild(document.getElementById(r_array[i] + id[3]));
    }
}

With this code im trying to detele some input elements

for r = 0 to a_rows-1 step 1%>
<tr id="regels">
    <td width="auto" id="nr"> 
        <input type="text" name="artid" id="artnr<%=r%>" value="<%=records(1,r)%>" size="6">
    </td>
    <td id="ps">
        <input type="text" name="ps" id="ps<%=r%>" onfocus="this.blur()" value="<%=records(2,r)%>" size="7">
    </td>
    <td id="aan">
        <input type="text" name="aan" id="aan<%=r%>" onkeyup="sub(this.id,this.value);count()"  value="<%=records(3,r)%>" size="2">
    </td>
    <td id="des">
        <input type="text" name="omschr" id="des<%=r%>" value="<%=records(4,r)%>" size="50">
    </td>
    <td id="subs">
        <input type="text" name="subtots" id="subtots<%=r%>" onfocus="this.blur()" value="<%=records(5,r)%>"  size="7">
    </td>
    <td id="del">
        <div id="del<%=r%>" onclick="dell(this.id)" style="cursor:pointer;border:1px black solid;font-size:20px">-</div>
    </td>
</tr>
<% next

This comes out of the database of mine. I'm trying to delete the input elements by giving them a id with a number. The strange thing is: I can delete the first row, but after that it says: 'NotFoundError: DOM Exception 8' when i try to delete another one.

Please help me. If you need more info about my code or something, you can ask it


Solution

  • It's a lot easier not to hardcode the parent element. Get the parent straight from the original element reference:

    function dell(a) {
        var id = a.split('');
        var r_array = ["artnr", "ps", "aan", "des", "subtots", "del"];
        for (var i = 0; i < r_array.length; i++) {
            var element = document.getElementById(r_array[i] + id[3]);
            if (element) {
                element.parentNode.removeChild(element);
            }
        }
    }