Loop Javascript - 1 + input

0

Good evening, everyone!

I'm stuck in a Javascript loop, it's the following:

User types any positive number, example 15.

I need to make the number 1 up to 15 available on the console.

The problem is that I do not know how to increment a number that I do not know which one will be typed and limit this number from number 1 to typed.

Have you understood or are you confused?

Thank you.

    
asked by anonymous 27.01.2017 / 07:51

1 answer

2

Find the answer to your question below, I think the question would be how to find the value of input document.getElementById(ID_DO_ELEMENTO).value

document.getElementById('go').onclick = function() { //attribui um evento click para o button[go]
  var numero = document.getElementById('numero').value; //busca o valor dentro do input[numero]
  if (isFinite(numero)) { //verifica se e numero
    for (var x = 1; x <= numero; x++) { //loop de 1 ate numero introduzido
      console.log(x); //escreve na consola os valores de x
    }
  }
}
<h3>Loop Javascript - 1 + input</h3>
<input type="text" id="numero" placeholder="Introduza um numero" />
<button id="go">Go</button>
    
27.01.2017 / 08:14