Javascript programming

Asked

Viewed 51 times

-1

How can I create a function in Javascript that will have a parameter that will receive what the user type on the screen and want as return a Alert with what was typed by the user?

My question is how to get the function to take such inserted text.

  • Use the let oQueFoiDigitado = window.prompt("Text");

1 answer

2


In steps:

a) to receive input from the user and put in a variable:

const nome = prompt('Escreva um nome!');

b) display text that has been inserted:

alert(nome);

Everything together in a role would be:

function pedirNome() {
  const nome = prompt('Escreva um nome!');
  alert(`O nome inserido foi:\n\n${nome}`);
  return nome;
}

const nomeUtilizador = pedirNome();

// e aqui podes continuar a usar o que o utilizador inseriu
// dentro da variável "nomeUtilizador"

  • can explain more or less how I could reproduce this message box with a div ?

  • @Gabriel, can you be clearer? Do you want the text to appear in a div instead of an Alert? does that div already exist or are you going to create? Try to be as descriptive as possible to understand your problem/doubt.

  • Yes instead of an Alert a div. It will have the function that will return the message that the user typed, and the div should return the same. My doubt is in the part of the function return in the div.

  • @Gabriel has a look here for example: https://answall.com/a/104926/129

Browser other questions tagged

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