Why can’t I save several . value in the array?

Asked

Viewed 63 times

-1

//Variaveis Globais
var d = document;
var Nomes = [];




function processar(idTabela) //Inserir Linhas e Verificar se ja foi escrito o nome
{
  var newRow = d.createElement('tr');
  newRow.insertCell(0).innerHTML = d.getElementsByName('Nome')[0].value;
  newRow.insertCell(1).innerHTML = d.getElementsByName('Idade')[0].value;
  d.getElementById(idTabela).appendChild(newRow);
  Nomes.push = d.getElementsByName('Nome')[0].value;

  return false;

};
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>DOCUMENTO</title>
</head>

<body>

  <!--Formulario-->
  <form name="formulario" onsubmit="return processar('myTable')">
    Nome: <input type="text" name="Nome"> Idade: <input type="number" name="Idade">
    <input type="submit" value="Salvar">
  </form>

  <!--Tabela-->
  <table width='250' height='250'>
    <tbody id="myTable"></tbody>
  </table>


  <!--Adicionando Arquivos-->
  <script src="codigo.js"></script>
</body>

</html>

  • 2

    Gabriel, what do you mean several . value? Your Names.push line is wrong, push is an array method, the value needs to be sent in parentheses and not using assignment: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

1 answer

3


If you want to save all the names in your Names array instead of:

Nomes.push = d.getElementsByName('Nome')[0].value;

You should trade for this:

Nomes.push(d.getElementsByName('Nome')[0].value);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.