Advertisement
Patrikrizek

lesson-8-if-statement

Jun 22nd, 2022
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Greeting Page</title>
  8. </head>
  9. <body>
  10.     <h1 id="greet">👉Greeting will appear here.👈</h1>
  11.  
  12.     <!-- Form with web visitors input. -->
  13.     <form>
  14.         <label for="time">Enter the time:</label><br>
  15.         <input type="number" name="time" id="hours" min="0" max="23" step="1" autofocus value="12">
  16.        
  17.         <span>:00</span>
  18.         <br>
  19.         <br>
  20.        
  21.         <input type="button" value="Greet me!" onclick="greetMe()">
  22.     </form>
  23.    
  24.  
  25.     <script>
  26.         function greetMe() {
  27.             var inHours = document.getElementById('hours').value;
  28.             var answer = "Good ";
  29.  
  30.             if (inHours < 12) { answer = answer + "morning." }
  31.                else if (inHours < 18) { answer = answer + "afternoon."}
  32.                    else { answer = answer + "evening." }
  33.            
  34.            // Input Validation - check if entered hours are correct
  35.            if (inHours < 0 || inHours > 23) {
  36.                 var answer = "You entered incorrect hours!";
  37.                 document.getElementById('hours').value = 0;
  38.             }
  39.  
  40.             document.getElementById('greet').innerHTML = answer;
  41.         }
  42.     </script>
  43. </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement