3
Script
var opt = document;
function inserirLinha(id) {
var newRow = opt.createElement('tr');
newRow.insertCell(0).innerHTML = '<input type="button" value="X" onclick="removeLinha(this)" />';
newRow.insertCell(1).innerHTML = 'REMOVER';
newRow.insertCell(2).innerHTML = 'A LINHA';
opt.getElementById(id).appendChild(newRow);
return false;
}
function removeLinha(id) {
var apagar = opt.getElementsByTagName(id);
apagar.parentNode.parentNode.removeChild(apagar.parentNode);
}
<center>
<form onsubmit="return inserirLinha('minhaTabela')">
<table>
<tr>
<tbody id="minhaTabela"></tbody>
</tr>
</table>
<input type="submit" value="Adicionar" name="submit">
</form>
</center>
The above script has two distinct functions, one of them to know inserirLinha
which includes a new row in the already predefined table in HTML, but the function removeLinha
is not fulfilling its role, why?
In the console browser, accuses:
Typeerror: delete is null
The Debbuger points to:
apagar.parentNode.parentNode.removeChild(apagar.parentNode);
Tai a problem that little shows me what to solve.
Now I’m running out of time to see, but what I think is that in
onclick="removeLinha(this)"
, thethis
refers to the objectinput
(not to an ID), which is whygetElementById
returns null.– hkotsubo
And to delete a row from a table, you might want to use
deleteRow
(instead ofparentNode
, depending on the structure of the page, the result can vary greatly)– hkotsubo