Simple JavaScript.
var userName = prompt ("What's your name?");
document.write ("Hello " + userName + ".");
Is there an alternative way to get the user's input (name) than through a pop up box? How would I go about implementing a text box within the site to obtain the user's name?
How is this?
<section class="content">
<input id="name" type="text">
<button onclick="userName()">Submit</button>
<p id="user"></p>
<script>
function userName() {
var name = document.getElementById("name").value;
user.innerHTML = "Hello" + " " + name}
</script>
</section>
A simple piece of HTML to create a text field and a button:
<input id="name" type="text">
<button onclick="go()">Go</button>
And a simple script to go with it that is called when the button is clicked:
function go() {
var text = document.getElementById("name").value;
alert("The user typed '" + text + "'");
}
Working demo: http://jsfiddle.net/jfriend00/V74vV/
In a nutshell, you put data entry fields into your page. Then you hook up an event handler to some sort of user event (like the click of a button) and in that code, you fetch the value from the data entry field and do whatever you want with it.
If you want to read about all the different types of input
tags, read here.
You probably also want to read about document.getElementById("xxx")
which allows you to find elements in your page that you've assigned an id to and then fetch properties from them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With