Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JavaScript IF</title>
- </head>
- <body>
- <h1>Greet me</h1>
- <form>
- <label for="time">Enter the time:</label><br>
- <input type="number" name="time" id="hours" min="0" max="23" step="1" autofocus value="12">
- <span>:00</span>
- <br>
- <br>
- <!-- onclick call the funtion named "greetMe()" -->
- <input type="button" value="Geet me now!" onclick="greetMe()">
- </form>
- <h2 id="output"></h2>
- <script>
- // create function
- function greetMe() {
- var inHours = document.getElementById("hours").value;
- var answer = "Good ";
- // if statement: check hours and based on value proceed with relevant operation
- if( inHours < 12 ) { answer = answer + "morning!"; }
- else if ( inHours < 18 ) { answer = answer + "afternoon!" }
- else { answer = answer + "evening!" }
- // if statement: check if the input value is in the range between 0 - 23 hours
- if ( inHours < 0 || inHours > 23 ) {
- var answer = "Input is incorect. The hours range is from 0 - 23.";
- document.getElementById("hours").value = 0;
- }
- document.getElementById("output").innerHTML = answer;
- }
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement