AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Web Development - Javascript - Prompt Guide

Javascript Prompt Guide



Example prompt dialog

The prompt box allows the user to enter information. The benefits of using a prompt is fairly limited and the use of forms would be preferred (from a user perspective). However, it exists and you are here to find out how it works.

The prompt box title text cannot be changed, the same applies to button text. You can have 2 lines of text using \n where the new line starts (please note that opera up to v7 will only display 1 line of text)

The text entry field is similar to a form input type="text" The 2 buttons are OK and Cancel. The value returned is either the text entered or null.

For demo purposes all example will output to an alert box - but on your pages there is nothing to stop you from updating incorrect form fields by providing the prompt dialog

The syntax for the prompt is
prompt("your message",""); (script tags omitted)
"your message","" the ,"" holds the default text value "" = empty.


Let's look at an example. Open a prompt

Here's the code. (written longhand for clarity)

<script language="JavaScript" type="text/javascript"> 
<!-- 
function example_1(){ 
var your_name = prompt("What is your name ?",""); 
if (your_name == null ){ // Cancel pressed 
alert("Ok then don't tell me :p"); 

else if (your_name == "" || your_name == " "){ // OK pressed but no text 
alert("What you don't have a name - how odd :)"); 

else 
{// OK pressed and theres something entered 
alert("Hello, "+ your_name); 


// -->
 
</script> 
  <a href="#" onClick="example_1();return false;">Open a prompt</a>

Here is another example

The code (written longhand for clarity)

<script language="JavaScript" type="text/javascript"> 
<!-- 
function example_2(){ 
var first_name = prompt("What is your FIRST name ?",""); 
var last_name = prompt("What is your LAST name ?",""); 
 
if (first_name == null ){ // Cancel pressed 
name1 = "Declined to answer"; 

else if (first_name == "" || first_name == " "){ // OK pressed but no text 
name1 = "What you don't have a first name - how odd :)"; 

else 
{// OK pressed and theres something entered 
name1 = first_name 

 
if (last_name == null ){ // Cancel pressed 
name2 = "Declined to answer"; 

else if (last_name == "" || last_name == " "){ // OK pressed but no text 
name2 = "What you don't have a last name - how odd :)"; 

else 
{// OK pressed and theres something entered 
name2 = last_name; 

 
alert("First Name : " + name1 + "\nLast Name : " + name2); 
 

// -->
 
</script> 
<p>Here is <a href="#" onClick="example_2();return false;">another example</a>

So there you have it, 2 examples of prompt box usage. How you use the prompt obviously depends on your requirements but whatever the use, the examples above show how to use the prompt in a useful way.

Let's move on to the confirm box


intro | alert | prompt | confirm | final chapter








Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]