codescracker


javascript

JavaScript prompt Box



« Previous Tutorial Next Tutorial »


The prompt box in JavaScript contains a text box along with OK and Cancel buttons. It returns the text string when OK is clicked and null when Cancel is clicked.

JavaScript prompt Box Example

Here is an example demonstrates prompt box in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
	<title>JavaScript prompt Box</title>
</head>
<body>

<h3>JavaScript prompt Box Example</h3>
<script type="text/javascript">
	
	var myname = prompt("Please enter your name: ");
	if(myname == null || myname == "") myname = " visitor ";
	{
		document.write("Hi " + myname + ", Welcome to codescracker.com");
	}
	
</script>

</body>
</html>

Here are some sample runs of the above JavaScript prompt box example. This is the initial output:

javascript prompt box

Now, enter your name as shown in the following snapshot:

javascript prompt box example

After entering your name, just press OK button. Here is the output produced after pressing the OK button:

prompt box javascript

In case, if your forgot to fill the text box and just pressed OK button, then here is the sample output produced after performing this action:

prompt box

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test