Working with Basic Form Elements

Lecture Overview

HTML Forms

  <body>
    <form> </form>
  </body>

The <input> Element

Text Input Field

  <form>
    First name:
      <br>
        <input type="text" id="firstname">
      <br>
    Last name:
      <br>
        <input type="text" id="lastname">
  </form>

Text Input Submit Button

<form id="myform" action="JavaScript:myFunctionName()">
  First name:<br>
  <input type="text" id="firstname" ><br>
  Last name:<br>
  <input type="text" id="lastname" ><br><br>
  <input type="submit" value="Submit">
</form>

Responsive Forms

 <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" cont`ent="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Name Echoer</title>

        <script type="text/javascript">
            function evaluateUserInput(){
                let userInputFirstName = document.getElementById("first_name").value;
                let userInputLastName = document.getElementById("last_name").value;
                let outputDisplay = "Your name is: " + userInputFirstName + " " + userInputLastName;
                document.getElementById("output").innerHTML = outputDisplay;
                return false;
            }
        </script>
    </head>
    
    <body>
        <form id="myform">
          First name:<br><input id="first_name" type="text"><br>
          Last name:<br><input id="last_name" type="text"><br>
          <button id="submit_button" onClick="return evaluateUserInput();">Submit</button>
        </form> 
        <p id="output">placeholder text until the `submit_button` is clicked</p>
    </body>
</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>Favorite Fruit Selector</title>

        <script type="text/javascript">
            function evaluateUserInput(){
                let userName = document.getElementById("user_name").value;
                let selectedFruit = document.getElementById("fruit_select").value;
                let outputDisplay = "Hello, " + userName + "! Your favorite fruit is: " + selectedFruit;
                document.getElementById("output").innerHTML = outputDisplay;
                return false;
            }
        </script>
    </head>
    
    <body>
        <form id="myform">
          <!-- Label for the text input -->
          <label for="user_name">Enter your name:</label><br>
          <input id="user_name" type="text" /><br><br>

          <!-- Label for the select dropdown -->
          <label for="fruit_select">Choose your favorite fruit:</label><br>
          <select id="fruit_select">
            <option value="Apple">Apple</option>
            <option value="Banana">Banana</option>
            <option value="Cherry">Cherry</option>
          </select><br><br>

          <!-- Button to trigger the action -->
          <button id="submit_button" type="submit" onClick="return evaluateUserInput();">Submit</button>
        </form> 
        <p id="output">placeholder text until the `submit_button` is clicked</p>
    </body>
</html>

Key Elements: