Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá.
Estou usando o exemplo abaixo para criar linhas na Table com um botão:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="[http://www.w3.org/1999/xhtml">](http://www.w3.org/1999/xhtml)
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script language="javascript">
function inserirLinhaTabela() {
// Captura a referência da tabela com id “minhaTabela”
var table = document.getElementById("minhaTabela");
// Captura a quantidade de linhas já existentes na tabela
var numOfRows = table.rows.length;
// Captura a quantidade de colunas da última linha da tabela
var numOfCols = table.rows[numOfRows-1].cells.length;
// Insere uma linha no fim da tabela.
var newRow = table.insertRow(numOfRows);
// Faz um loop para criar as colunas
for (var j = 0; j < numOfCols; j++) {
// Insere uma coluna na nova linha
newCell = newRow.insertCell(j);
// Insere um conteúdo na coluna
newCell.innerHTML = "Linha "+ numOfRows + " – Coluna "+ j;
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<input name="button" type="button" onclick="inserirLinhaTabela()" value="Inserir Linha" />
</form>
<form id="form2" name="form2" method="post" action="">
<table id="minhaTabela" border="1">
<tr>
<td>
Linha 0 - Coluna 0
</td>
<td>
Linha 0 - Coluna 1
</td>
</tr>
</table>
</form>
<p> </p>
</body>
</html>
Sei que o comando abaixo remove linhas da Table:
document.getElementById('myTable').deleteRow(i)
Minha dificuldade está em identificar qual a linha que o usuário deseja deletar. Teria por exemplo, jeito de colocar um botão EXCLUIR por linha da table e assim que clicasse nele faria o deleteRow correspondente?
Grato!
Carregando comentários...