Utilizar DOM para inserir novas linhas em tabela.
Boa noite galera
Estou com dificuldades de inserir o valor da variável <nome> entre os TD's da minha ultima linha da tabela.
É obrigatório o uso do DOM, mesmo acessando o nó que quero, não estou conseguindo fazer com que seja setado o valor da variavel na referida coluna.
<html>
<head>
<title>--DOM - Exercicios--</title>
<meta charset="utf-8" /> </head>
<body>
<div> Nome
<input id="nome">
<br> Data Nascimento
<input id="dn">
<br> E-mail
<input id="email">
<br>
<button onclick="Enviar()">Enviar</button>
</div>
<script type="text/javascript">
function Enviar() {
var nome = document.getElementById("nome").value;
var dn = document.getElementById("dn").value;
var email = document.getElementById("email").value;
var tabela = document.getElementById("tabela");
var nova_linha = document.createElement("tr");
for (i = 0; i < 5; i++) {
var nova_col = document.createElement("td");
nova_linha.appendChild(nova_col);
}
tabela.appendChild(nova_linha);
var ultima_linha = document.getElementsByTagName("tr")[1].firstChild; //retorno meu primeiro td (nome)
ultima_linha.nodeValue = nome; // coloco o valor de nome na coluna
}
</script>
<table id="tabela" border="1">
<tr>
<td>Nome</td>
<td>Data Nascimento</td>
<td>Idade</td>
<td>Email</td>
<td>Acoes </td>
</tr>
</table>
</body>
</html>Discussão (3)
Carregando comentários...