91 lines
3.0 KiB
HTML
91 lines
3.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Women4IT - JavaScript</title>
|
|
<meta name="description" content="Women4IT JavaScript Exercise: Variables -1">
|
|
<!-- Bootstrap CSS -->
|
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
|
<!-- Customise Bootstrap for practical grid layouts -->
|
|
<link rel="stylesheet" href="../assets/css/bootstrap-custom.css">
|
|
<style>
|
|
.container { padding-top: 40px }
|
|
.js-box {
|
|
border:solid 1px yellow;
|
|
padding:20px;
|
|
background-color: lightyellow;
|
|
margin-bottom: 48px;
|
|
font-size: larger;
|
|
}
|
|
#resultBox, #resultNum {
|
|
border: solid 1px green;
|
|
height: 40px;
|
|
background-color: #E7FFE7;
|
|
width: 480px;
|
|
padding: 3px 12px;
|
|
}
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
|
|
<body>
|
|
<div class="container col-md-8 offset-md-2">
|
|
<h1><b>Exercise 7-3</b>: JavaScript Conditional Statements</h1>
|
|
<p>This script tests user input with the <i>if</i> and <i>else if</i> commands.</p>
|
|
|
|
<h3>Using <i>if</i>, <i>else if</i> and <i>else</i> statements</h3>
|
|
|
|
<div class="js-box">
|
|
<p>Welcome, visitor. What country are you from?</p>
|
|
<p>Country Name: <input type="text" id="countryName"></p>
|
|
<p><button id="btnUserCountry">Show greeting</button></p>
|
|
|
|
<div id="resultBox"></div>
|
|
|
|
</div>
|
|
|
|
<script>
|
|
|
|
function userCountryGreeting() {
|
|
/* Place the country name from the input field in a variable */
|
|
let userCountry = document.getElementById("countryName").value;
|
|
let userGreeting;
|
|
/* Display the appropriate greeting */
|
|
if (userCountry == "France") {
|
|
userGreeting = "🇫🇷 Bonjour";
|
|
}
|
|
|
|
else if (userCountry == "Germany") {
|
|
userGreeting = "🇩🇪 Guten tag";
|
|
}
|
|
|
|
else if (userCountry == "Italy") {
|
|
userGreeting = "🇮🇹 Ciao";
|
|
}
|
|
else {
|
|
userGreeting = "🌎 Wherever you are from, welcome to my website";
|
|
}
|
|
|
|
document.getElementById("resultBox").innerHTML = userGreeting;
|
|
}
|
|
</script>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<script>
|
|
document.getElementById("btnUserCountry").addEventListener("click", userCountryGreeting);
|
|
</script>
|
|
|
|
<!-- jQuery library -->
|
|
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
|
|
<!-- Popper JS -->
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
|
|
<!-- Latest compiled JavaScript -->
|
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
|
|
</body>
|
|
</html> |