Wednesday, April 23, 2025

JavaScript Prompt Box, User Input

The prompt box displays a message to the user and provides a text field for the user to enter a response. 

Prompt boxes are useful in JavaScript when you need to prompt the user for input data, such as asking for their name, age, or other personal information. Prompt boxes are also commonly used to confirm an action or prompt the user for a yes or no response.

Here are a few examples of where prompt boxes can be used in JavaScript:

  • Collecting user input: You can use a prompt box to ask the user for input data, such as their name, age, or email address.

  • Confirming an action: You can use a prompt box to confirm an action before proceeding, such as deleting a file or closing a window.

  • Validating input: You can use a prompt box to validate user input, such as checking if the user has entered a valid email address or phone number.

  • Customizing user experience: You can use a prompt box to customize the user experience, such as asking the user to choose their preferred language or theme.

Prompt boxes are a simple and effective way to prompt the user for input data and improve the interactivity of your JavaScript applications.


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>

<body>

<script>
	
	var grab_stuff = prompt("Your Name: ");
	
	document.write(grab_stuff + "<br>");
	
</script>
	
</body>
</html>

This script displays a prompt box that asks the user to enter their name using the prompt() method. The entered value is stored in the grab_stuff variable.

Then, the entered value is output to the webpage using the document.write() method, followed by a line break using the <br> tag.

When the script is run, it displays a prompt box with a message "Your Name: ", and the user can enter their name into the text field. Once the user clicks "OK", the entered value is stored in the grab_stuff variable and output to the webpage. 


<script>
	
	var grab_stuff = prompt("Your Name: ");
	
	for (x = 0; x < 5; x = x + 1) {
		document.write(grab_stuff + "<br>");
	}
	
</script>

This script displays a prompt box that asks the user to enter their name using the prompt() method. The entered value is stored in the grab_stuff variable.

Then, a for loop is used to output the entered value to the webpage five times using the document.write() method, followed by a line break using the <br> tag. The loop increments the variable x by 1 on each iteration until it reaches a value of 5.

When the script is run, it displays a prompt box with a message "Your Name: ", and the user can enter their name into the text field. Once the user clicks "OK", the entered value is stored in the grab_stuff variable and output to the webpage five times using the for loop. Each time the name is output, it is followed by a line break using the <br> tag.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...