Learn all you need to know about English!
On this page, we work through three basic JavaScript examples, showing the code extracts and explaining how it works.
Through these examples, you will learn how to get and respond to user input.
You can also copy the code extracts from the grey code boxes and try them on your own website.
This example shows a simple alert box when a button is clicked
The script:
<button id="myButton">Click me</button>
<script>
myButton.addEventListener("click", test1);
function test1() {
alert("Hello! I am an alert box!");
}
</script>
id="myButton"
attached. This button is an HTML object and can be placed anywhere on the page.<script>
tags.myButton.addEventListener("click", test1);
. The event is "click"
and the action performed by the click is the function named test1
.function
keyword. The function is named test1()
, and it performs the action enclosed within { ... }
. This function performs an alert()
, which presents a pop-up box.alert()
object we have the sentence "Hello! I'm an alert box!", which will display to the user.This example shows how JS can manipulate text on a document.
Watch this text change.
The script:
<button id="button2">Click this button and watch the text below change.</button>
<p style="font-weight:bold;" id="demo1">Watch this text change.</p>
<script>
button2.addEventListener("click", test2);
function test2() {
document.getElementById("demo1").innerHTML = "Hello World! How are you today?";
}
</script>
document.getElementById("demo1").innerHTML =
document.getElementById("demo1")
that has an id named "demo1".innerHTML =
. The phrase after the =
sign is then displayed inside the <p>
tag with id="demo1"
.This example shows how JS can take user input and join it to another text and output it.
When you enter a name and submit, a message will be displayed underneath.
Enter your name, or any name:
The message will appear here:
The script:
<p>Enter your name, or any name: <input type="text" id="inputName">
<button id="button3">Submit</button>
</p>
<p>The message will appear here: <span id="output1"></span></p>
<script>
button3.addEventListener("click", test3);
function test3() {
var yourName = document.getElementById("inputName").value;
document.getElementById("output1").innerHTML = "Hello " + yourName + ". How are you today?";
}
</script>
input type="text"
with an ID, id="inputName"
is used for user input.getElementById().value;
value
is whatever is written in the input box.var
) named yourName
.+
operator, and writes it to the document inside the page element with id "output1".+
operator is known as ‘concatenation’.But what happens if the user doesn't enter a name?
Try the above example again, without inputting a name. You will see a message saying only 'Hello' with no name.
We can solve this by making sure the user enters a name:
Enter your name:
The message will appear here:
The script
<p>Enter your name: <input type="text" id="inputName2"><button id="button4">Try it</button></p>
<p>The message will appear here: <span id="output2"></span></p>
<script>
button4.addEventListener("click", test4);
function test4() {
var yourName = document.getElementById("inputName2").value;
if(!yourName){
alert("Please enter your name!");
}
else {
document.getElementById("output2").innerHTML = "Hello " + yourName;
}
}
</script>
JavaScript provides a core programming construct known as an 'if / else' condition. We can use an if
condition to check that the text box contains a value. Look at the if
and else
conditions in the code above:
if
statement checks whether the variable yourName
contains a value.!
, placed before the variable name.!yourName
is true), show an alert box.if
statement is followed by an else
statement, which executes if the first if
statement is false (i.e., if yourName
is not empty).else
statement writes the value to the document.This is the end of this brief introduction to JavaScript. If you want to proceed, head over to my random sentence maker to learn some more JavaScript concepts.