Is there any way for the user to write text in JavaScript without pop-ups?

6

I'm starting in JavaScript, I know you can ask the user to insert any text with the code:

prompt("Inserta lo que quieras");

The problem with this is that a pop-up window opens and I was wondering if there was any way to do the same, but that it was displayed on the page itself, without pop-ups.

    
asked by Neosss 15.01.2016 в 14:49
source

2 answers

6

For that, the HTML tag has the input specifically type="text"

Example to request the name of the user

<input type="text" id="nombre" />

The easiest way to reference the input from JavaScript is to assign it an id (in this example its value is name), although there are other ways.

Then from javascript you can do something like:

var nombre = document.getElementById("nombre").value;

Here I leave you a simple executable example:

function obtenerNombre() {
  var nombre = document.getElementById("nombre").value;
  
  alert(nombre);
}
<input type="text" id="nombre" />

<button onclick="obtenerNombre()">Obtener el nombre</button>
    
answered by 15.01.2016 / 14:52
source
2

Yes, in the DOM itself you can embed a input ( text , textarea ...) and then capture the value that the user has entered with Javascript. You can achieve this by using a jQuery type library or directly accessing the DOM with functions type document.getElementById

link

In that simple fiddle (press Run up on the left) you can see a concise example of how to use the value of a user entered in a text box.

    
answered by 15.01.2016 в 14:56