Prompting and Prodding

prompt

Poking and prodding to get the information we want…this is usually something we do with our teenagers, but we can prompt our end users for information by using built in funcionality within JavaScript. The function/method we are going to use is entitled, prompt().

Now one caveat before we dive into this subject…this function is not one I would suggest using often. There are some pitfalls with it, especially when it comes to validation of data. Nevertheless, it is a handy function to know when you want quick feedback. Here is how it works…

[code lang=”js”]
// Create variable to house end user info var userInfo;
// Prompt the user to enter information
userInfo = prompt("Please enter some information.");
// Output the end user’s information to the screen
document.write("You entered: " + userInfo);
[/code]

So, as you can see from the code above, we can prompt the end user for some information. They can then enter that information. We can store it in a variable and then do what we would like to do with it. Below is what the end user would see when our code runs…

prompt

prompt with information

prompt screen display

So, we can quickly and easily gather information from our users. I mentioned previously about some pitfalls with this. I will be honest, I do not use this too often because of the pitfalls. Forms are a better way of gathering the necessary information. Within a prompt, there is not a way to limit or dictate what the end user puts in. It is simply a text box with no parameters. Speaking of being a text box, numbers can be an issue. If you ask an end user to enter a number, that number comes across as a data type of string. So it needs to be converted to an integer using the Number() function.

With all of the issues and trappings that surround the prompt() that limits its usage, it is still a handy, quick little function in certain circumstances.

Happy Coding!

More To Explore

Code

The Contact Picker API: Fast, private access to a user’s address book

The Contact Picker API lets mobile web apps open the native contacts chooser so users can share exactly the fields you request—like name, phone, email, address, or avatar—without granting blanket access to their entire address book. It’s user-initiated, privacy-preserving, and perfect for speeding up invites and form fills.

Focused software developer debugging code on multiple screens in dark environment
Code

Mastering the Console API: A Developer’s Best Debugging Friend

Ever find yourself relying solely on console.log() for debugging? You’re missing out on the Console API’s full potential. This powerful debugging toolkit offers specialized methods for timing operations, inspecting complex objects, tracing function calls, and organizing your debugging output. In just minutes, you can elevate your troubleshooting skills from basic to professional-grade. Let’s explore how the Console API can transform your development workflow and help you solve bugs more efficiently than ever before.

Share This Post

small_c_popup.png

Need help?

Let's have a chat...


Login

Jump Back In!

Here at Webolution Designs, we love to learn. This includes sharing things we have learned with you. 

Register

Begin Your Learning Journey Today!

Come back inside to continue your learning journey.