DONT ADD ANYTHING HERE!

The input element is used to create interactive controls for web-based forms in order to accept data from the user
  1. The input element is one of the most powerful and complex in all of HTML due to the sheer number of combinations of input types and attributes
  2. A wide variety of types of input data and control widgets are available, depending on the device and user agent

Example (type=text)

HTML

<label for="name">Name (4 to 8 characters):</label>

<input type="text" 
       id="name" 
       name="name" 
       required 
       minlength="4" 
       maxlength="8" 
       size="10" />
                            

Browser


Example (type=button)

HTML

<input 
    type="button" 
    value="Add to favorites" />

Browser

Notes

  1. input elements of type=button are rendered as simple push buttons
  2. They have no default behavior
  3. Their cousins, input type=submit, and input type=reset are used to submit and reset forms, respectively
  4. May be progammed with JS by assigning an event handler

Example (type=checkbox)

HTML

<fieldset>
    <legend>Choose your monster's features:</legend>

    <div>
        <input type="checkbox" id="scales" name="scales" checked />
        <label for="scales">Scales</label>
    </div>

    <div>
        <input type="checkbox" id="horns" name="horns" />
        <label for="horns">Horns</label>
    </div>
</fieldset>

Browser

Choose your monster's features:

Notes

  1. Rendered by default as boxes that are checked (ticked) when activated
  2. value
    1. A string representing the value of the checkbox
    2. This is not displayed on the client-side, but on the server this is the value given to the data submitted with the checkbox's name
    3. The value attribute is one which all input elements share
    4. However, it serves a special purpose for inputs of type checkbox
      1. When a form is submitted, only checkboxes which are currently checked are submitted to the server, and the reported value is the value of the value attribute.
      2. If the value is not otherwise specified, it is the string on by default
  3. checked
    1. A boolean attribute indicating whether this checkbox is checked by default (when the page loads)
    2. It does not indicate whether this checkbox is currently checked
      1. If the checkbox's state is changed, this content attribute does not reflect the change
      2. Only the HTMLInputElement's checked IDL attribute is updated
  4. Radio buttons are similar to checkboxes, but with an important distinction
    1. Same-named radio buttons are grouped into a set in which only one radio button can be selected at a time, whereas checkboxes allow you to turn single values on and off
    2. Where multiple same-named controls exist, radio buttons allow one to be selected out of them all, whereas checkboxes allow multiple values to be selected.

Example (type=color)

HTML

<p>Choose your colors:</p>

<div>
    <input type="color" id="bgColor" name="bgColor" value="#e66465" />
    <label for="bgColor">Background Color</label>
</div>

<div>
    <input type="color" id="txtColor" name="txtColor" value="#ffffff" />
    <label for="txtColor">Color</label>
</div>

<div id="change">Hello World! </div>

Javascript

Reveal Code

    // color select
    document.querySelector("#change").style.backgroundColor = document.querySelector("#bgColor").value
    document.querySelector("#change").style.color = document.querySelector("#txtColor").value
    document.querySelector("#change").style.textAlign = "center";

    document.querySelector("#bgColor").addEventListener('input', function () {
        document.querySelector("#change").style.backgroundColor = this.value;
    })

    document.querySelector("#txtColor").addEventListener('input', function () {
        document.querySelector("#change").style.color = this.value;
    })

Browser

Choose your colors:

Hello World!

Example (type=date)

<label for="start">Start date:</label>
<input 
    type="date" 
    id="start" 
    name="trip-start" 
    value="2018-07-22" 
    min="2018-01-01"
    max="2018-12-31" />

Browser


Example (type=email)

Notes

  1. input elements of type email are used to let the user enter and edit an email address, or, if the multiple attribute is specified, a list of email addresses
  2. The input value is automatically validated to ensure that it's either empty or a properly-formatted email address (or list of addresses) before the form can be submitted.
  3. The :valid and :invalid CSS pseudo-classes are automatically applied as appropriate to visually denote whether the current value of the field is a valid email address or not.
  4. value
    The input element's value attribute contains a string which is automatically validated as conforming to email syntax. More specifically, there are three possible value formats that will pass validation:
    1. An empty string ("") indicating that the user did not enter a value or that the value was removed.
    2. A single properly-formed email address. Checks only if email is formatted correctly: username@domain or username@domain.tld. Can also validate with regular expression
    3. If the multiple attribute is specified, the value can be a list of properly-formed comma-separated email addresses
  5. maxlength
    The maximum string lengththat the user can enter into the email input
  6. minlength
    The minimum string length that the user can enter into the email input. This must be a non-negative integer value smaller than or equal to the value specified by maxlength
  7. multiple
    A Boolean attribute which, if present, indicates that the user can enter a list of multiple email addresses, separated by commas and, optionally, whitespace characters
  8. size
    1. The size attribute is a numeric value indicating how many characters wide the input field should be
    2. The value must be a number greater than zero, and the default value is 20
    3. This does not set a limit on how many characters the user can enter into the field. It only specifies approximately how many can be seen at a time.
  9. placeholder
    The placeholder attribute is a string that provides a brief hint to the user as to what kind of information is expected in the field

Example (type=file)

HTML

  <form 
      method="post" 
      action="https://mysite.tom" 
      enctype="multipart/form-data">
      <div>
        <label for="myfile">Choose file to upload</label>
        <input type="file" id="myfile" name="myfile">
      </div>
      <div>
        <input type="submit">
      </div>
  </form> 

Browser

Notes

  1. Three requirements for file upload
    1. input type=file
      Provides access to user's filesystem
    2. method=POST
    3. enctype="multipart/form-data"
  2. accept
    1. A string that defines the file types the file input should accept.
    2. This string is a comma-separated list of unique file type specifiers.

Example (type=hidden)

HTML

<input 
    type="hidden" 
    id="name" 
    name="param1" 
    value="Hello">

<input 
    id="mybtn" 
    type="image" 
    src="../images/myButton2.png" 
    alt="Blue Button">

Notes

  1. input elements of type hidden let web developers include data that cannot be seen or modified by users when a form is submitted
  2. Hidden inputs are completely invisible in the rendered page, and there is no way to make it visible in the page's content.
  3. While the value isn't displayed to the user in the page's content, it is visible—and can be edited—using any browser's developer tools or "View Source" functionality

Example (type=image)

HTML

<input 
    type="image" 
    src="../images/myButton2.png" 
    alt="click me"
    formaction="https://phptwo.tomgdow.com" 
    formmethod="GET">
                        

Notes

  1. input elements of type image are used to create graphical submit buttons, i.e. submit buttons that take the form of an image rather than text
  2. (MDN) When you submit a form using a button created with input type="image" extra data points are submitted to the server automatically by the browser — x and y
  3. value
    1. input elements of type images do not accept value attributes
    2. The path to the image to be displayed is specified in the src attribute
  4. alt
    The alt attribute provides an alternate string to use as the button's label if the image cannot be shown
  5. formaction
    1. A string indicating the URL to which to submit the data
    2. This takes precedence over the action attribute on the form element d that owns the input
    3. This attribute is also available on input type=submit and button elements
  6. formenctype
    1. A string that identifies the encoding method to use when submitting the form data to the server
    2. There are three permitted values
      1. application/x-www-form-urlencoded
        This, the default value, sends the form data as a string after URL encoding (percent encoding) the text using an algorithm such as encodeURI()
      2. multipart/form-data
        1. Uses the FormData API to manage the data, allowing for files to be submitted to the server.
        2. You must use this encoding type if your form includes any input elements of type file
      3. text/plain
        Plain text; mostly useful only for debugging, so you can easily see the data that's to be submitted
    3. If specified, the value of the formenctype attribute overrides the owning form's action attribute
  7. formmethod
    1. A string indicating the HTTP method to use when submitting the form's data
    2. This value overrides any method attribute given on the owning form
    3. Permitted values are:
      1. get
      2. post
      3. dialog
  8. formtarget
    1. A string which specifies a name or keyword that indicates where to display the response received after submitting the form
    2. The string must be
      1. A valid browser context
      2. _self
      3. _blank
      4. _parent
      5. _top

Example (type=number)

HTML

<label for="number">Number (0-100, step 10):</label>

<input 
    type="number" 
    placeholder="x10" 
    step="10" 
    min="0" 
    max="100" 
    id="number" />
    
<output id="mydiv"></output> 

JavaScript

document.getElementById("number").addEventListener('input', function () {
    document.getElementById('mydiv').value = this.value;
}) 

Browser

Notes

  1. input elements of type number are used to let the user enter a number
  2. They include built-in validation to reject non-numerical entries

Example (type=password)

HTML

<form action="https://php.tomgdow.com" method="post" id="form_one">

    <label for="login-name">name</label>
    <input type="text" id="login-name" name="param1">

    <label for="password">password</label>
    <input type="password" id="password" name="param2" required>
    
    <input type="submit" value="Submit">

</form>
<div>
    <output id="out-login-name"></output>
    <output id="out-password"></output>
</div> 

JavaScript

code
    async function sendData() {

        const formData = new FormData(form_one);

        try {
            const response = await fetch("https://php.tomgdow.com", {
                method: "POST",
                body: formData
            });

            let result = await response.text();
            console.log(result);
            let responseArray = result.split('&');
            document.getElementById('out-login-name').value = responseArray[0];
            document.getElementById('out-password').value = responseArray[1];

        } catch (e) {
            console.error(e);
        }
    }

    document.getElementById('form_one').addEventListener('submit', function (event) {

        event.preventDefault();

        sendData();


    });
    document.getElementById('login-name').addEventListener('focus', function () {
        document.getElementById('out-login-name').value = "";
        document.getElementById('out-password').value = "";
    })

    document.getElementById('password').addEventListener('focus', function () {
        document.getElementById('out-login-name').value = "";
        document.getElementById('out-password').value = "";
    })

                                

Browser



Notes

  1. A single-line text field whose value is obscured
  2. Will alert user if site is not secure

Example (type=reset)

HTML

<form>
    <div class="controls">
        <label for="id">User ID:</label>
        <input type="text" id="id" name="id" />

        <input type="reset" value="Reset" />
        <input type="submit" value="Submit" />
    </div>
</form> 

Browser

Notes

  1. input elements of type reset are rendered as buttons, with a default click event handler that resets all inputs in the form to their initial values.

Example (type=search)

HTML

<label for="site-search">Search the site:</label>
<input type="search" id="site-search" name="q" />

<button>Search</button>

Browser

Notes

  1. input elements of type search are text fields designed for the user to enter search queries into
  2. These are functionally identical to text inputs, but may be styled differently by the user agent

Example (type=url)

HTML

<form>
    <label for="url">Enter a URL:</label>
    <input type="url" name="url" id="url" placeholder="https://example.com" pattern="https://.*" size="30" required />
</form> 

Browser

Notes

  1. input elements of type url are used to let the user enter and edit a URL
  2. The input value is automatically validated to ensure that it's either emtpty string or a properly-formatted URL before the form can be submitted
  3. On browsers that don't support inputs of type url, a url input falls back to being a standard text input

Notes

Mnemonic

References