user4548858
user4548858

Reputation: 45

How do I get input from an alert?

I am trying to take input from an alert, but when I try to assign it, it gives me a button alert.

x = alert('Enter: ');
alert(x);
<!DOCTYPE html>
<html>
  </html>

Upvotes: 1

Views: 105

Answers (2)

Donovan Solms
Donovan Solms

Reputation: 961

alert doesn't allow for input, but prompt does.

var username = prompt("Enter name", "");

if (username != null) {
    alert("Welcome " + username + "!");
}

Upvotes: 0

A.J. Uppal
A.J. Uppal

Reputation: 19264

Use prompt instead of alert in your code.

Syntax:

window.prompt("sometext","defaultText");


x = prompt("Enter text")
alert(x);
<!DOCTYPE html>
<html>
  </html>

Also, try a google search (e.g. "alert input") and the first option will give you your answer.

Upvotes: 5

Related Questions