Javascript Tutorial - Getting User Input with Prompt and Confirm

Skill

Javascript Tutorial - Getting User Input with Prompt and Confirm

Posted in:

There's lots of times when you might want to ask a user a simple yes or no question, or ask for some text input. In this tutorial, I'm going to demonstrate how to use the confirm and prompt functions in javascript to get feedback from your users.

There aren't many places on the web that use either of these functions. Google Reader is a place I see one of them most often. If you attempt to mark all unread items in a feed as read, and there are several items in the feed, Google will ask you if you're sure you want to do this. It's a perfect example of when you'd want to use the confirm function. As for the prompt function, well I don't think I've every actually seen it in use, but it's useful to know nonetheless.

Let's start with the confirm function, which will present a question to your user and ask them to respond with "OK" or "Cancel".

So how did I make this example? Well, it's painfully easy. It's so simple, I'll just throw all of the code into a single block.

<script type="text/javascript">
function confirmInput()
{
  var ans = confirm("Install a virus and delete all your files?");

  alert(ans ? "You are brave.  For that, I will not delete your files."
              : "Too bad, I installed it anyway.");
}
</script>

<input type="button" onclick="confirmInput()" value="Show me the Question!" />

The first thing I need is a function that is called when the button is clicked. I called mine confirmInput. Inside that is where the magic happens. The function confirm simply takes the question you want to ask as a string and returns a boolean indicating the answer. Normally, based on the return value of confirm, you'd either do or not do what you just asked the user. Here I just alerted some text to indicate the answer. After the javascript code, is just the button itself - simple and straight forward.

All right, let's move on to the asking the user for some text input using the prompt function.

Just like with confirm, prompt is very easy to use.

<script type="text/javascript">
function getInfo()
{
  var ans = prompt("What the answer to life, the universe, and everything?");

  alert(ans == "42" ? "Good job, you read a book!"
                      : "There's a book you should read.");
}
</script>

<input type="button" onclick="getInfo()" value="Ask me Something!" />

Instead of a boolean, prompt returns the string actually entered by the user. If the user clicks OK without entering anything, you'll receive an empty string. If the user clicks Cancel, the function will return null.

Well, there you go. You now know how to ask for all sorts of information using javascript. If you have questions or comments, leave them below. If you actually found a mainstream website that uses the prompt function, I'd love to hear about that also.

Bas
06/16/2008 - 10:17

People seem to avoid use of prompt because Internet Explorer blocks it since XP Service Pack 2. I think it is a good reason that prompt is not very common.

reply

The Fattest
06/16/2008 - 10:33

That does not seem to be true. I am running IE6 and SP2 and sample prompt above works just fine.

reply

The Reddest
06/16/2008 - 10:48

IE7 blocks it - the call immediately returns with null. It then adds that little bar at the top where you can allow it.

reply

Adam Carlson
09/24/2008 - 07:36

IE7 will not always block the Prompt dialog box. It depends on your Security settings.

reply

Valamas
06/17/2008 - 17:43

Using "I will install a virus and delete" as your sample message is not good. You should change it.

reply

Anonymous
06/10/2009 - 09:30

Valamas... Get a life and sense of humour

reply

Anonymous
11/05/2009 - 02:23

The Hitchhiker's Guide to the Galaxy, right?

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors