4

I think that the title explains itself. I need to save the input values in a object and then save this object in a array.

<html>
  <head>
    <title>Teste</title>
  </head>
  <script>
    function vai(){
      var pessoa = {
      nome = document.getElementById('nome').value,
      idade = document.getElementById('idade').value
    };
      var i = 0;
      var pessoas = [];
      pessoas[i] = pessoa{};
      i++;
    }

  </script>

  <body>
    <label>Digite seu nome aqui</label>
    <input type='text' id='nome' placeholder='aquii'>
    <label>Digite sua idade aqui</label>
    <input type='text' id='idade' placeholder='aquii'>
    <button onclick='vai()'>
      Teste aqui
    </button>

  </body>
</html>

2 Answers 2

3

You should declare the array in the global environment (outside the function). You also have some syntax issue inside the object (should be : instead of =). Then you can simply push() the object into the array in each button click.

Try the following way:

var pessoas = [];
function vai(){
  var pessoa = {
    nome: document.getElementById('nome').value,
    idade: document.getElementById('idade').value
  };
  pessoas.push(pessoa);
  console.log(pessoas);
}
<label>Digite seu nome aqui</label>
<input type='text' id='nome' placeholder='aquii'>
<label>Digite sua idade aqui</label>
<input type='text' id='idade' placeholder='aquii'>
<button onclick='vai()'>
  Teste aqui
</button>

You approach without using the push() by maintaining a variable is almost correct except the variable declaration scope. You have to declare that as a global variable. You can assign the object into the current position and increment that in each function call:

var pessoas = [];
var i = 0;
function vai(){
  var pessoa = {
    nome: document.getElementById('nome').value,
    idade: document.getElementById('idade').value
  };
  pessoas[i] = pessoa;
  i++;
  console.log(pessoas);
}
<label>Digite seu nome aqui</label>
<input type='text' id='nome' placeholder='aquii'>
<label>Digite sua idade aqui</label>
<input type='text' id='idade' placeholder='aquii'>
<button onclick='vai()'>
  Teste aqui
</button>

Please Note: You can also simply use the length property of the array as the current index in which case you do need to maintain the variable:

var pessoas = [];
function vai(){
  var pessoa = {
    nome: document.getElementById('nome').value,
    idade: document.getElementById('idade').value
  };
  pessoas[pessoas.length] = pessoa;
  console.log(pessoas);
}
<label>Digite seu nome aqui</label>
<input type='text' id='nome' placeholder='aquii'>
<label>Digite sua idade aqui</label>
<input type='text' id='idade' placeholder='aquii'>
<button onclick='vai()'>
  Teste aqui
</button>

Sign up to request clarification or add additional context in comments.

1 Comment

@aquasp, you are most welcome, you can refer the second answer in the updated answer if you do not want to use push():)
2

You need to fix three things:

  • Use : inside Object literals {} instead of =
  • Declare the main array pessoas in the global scope so it don't get change on every click
  • Use push() to add the object at the end of array.

Below is the working snippet.

var pessoas = [];

function vai() {
  var pessoa = {
    nome: document.getElementById('nome').value,
    idade: document.getElementById('idade').value
  };
  pessoas.push(pessoa)
  console.log(pessoas)
}
<label>Digite seu nome aqui</label>
    <input type='text' id='nome' placeholder='aquii'>
    <label>Digite sua idade aqui</label>
    <input type='text' id='idade' placeholder='aquii'>
    <button onclick='vai()'>
      Teste aqui
    </button>

2 Comments

Thanks! I didin't know the push() function. But just one more question, what if I wanted to save without push? Would this be possible?
@aquasp You can use length of the array pessoas[pessoas.length] = pessoa

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.