Advertisement
Patrikrizek

lesson-7-exampleif

Mar 24th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.55 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>JavaScript IF</title>
  8. </head>
  9. <body>
  10.     <h1>Greet me</h1>
  11.     <form>
  12.         <label for="time">Enter the time:</label><br>
  13.         <input type="number" name="time" id="hours" min="0" max="23" step="1" autofocus value="12">
  14.         <span>:00</span>
  15.  
  16.         <br>
  17.         <br>
  18.         <!-- onclick call the funtion named "greetMe()" -->
  19.         <input type="button" value="Geet me now!" onclick="greetMe()">
  20.     </form>
  21.  
  22.     <h2 id="output"></h2>
  23.  
  24.  
  25.     <script>
  26.  
  27.         // create function
  28.         function greetMe() {
  29.  
  30.             var inHours = document.getElementById("hours").value;
  31.             var answer = "Good ";
  32.  
  33.             // if statement: check hours and based on value proceed with relevant operation
  34.             if( inHours < 12 ) { answer = answer + "morning!"; }
  35.                else if ( inHours < 18 ) { answer = answer + "afternoon!" }
  36.                    else { answer = answer + "evening!" }
  37.  
  38.            // if statement: check if the input value is in the range between 0 - 23 hours
  39.            if ( inHours < 0 || inHours > 23 ) {
  40.                 var answer = "Input is incorect. The hours range is from 0 - 23.";
  41.                 document.getElementById("hours").value = 0;
  42.             }
  43.  
  44.             document.getElementById("output").innerHTML = answer;
  45.  
  46.  
  47.         }
  48.  
  49.     </script>
  50.  
  51.  
  52. </body>
  53. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement