This is a continuation of my HTML cheat sheet with a focus on the form elements.
the minimalist form
Forms start with a form ta that uses the format:
autocomplete="off">
<button type="submit" name="submit1" value="submit"
form="sample1">Sample Submit button</button>
<button type="submit" name="submit1b" value="submit"
formaction="https://spelchan.com/index.php">
Sample Submit button</button>
</form>
This looks like:
input
While can have anything in a form, to better tie text to the element that is receiving input, we use <label> to create labels with the for attribute linking the label to the input. Forms can have boxed areas in them by using <fieldset> with a title attached to the box by using the <legend> tag. Labels have a type as weill as the following attributes:
- autocaptialize for browsers to aid in input. None, character, word, sentence.
- autocomplete lets you turn off browser autocomplete.
- disabled disables the box
- form lets you specify which form input is for
- maxlength maximum number of characters
- minlength maximum number of characters for valid input
- placeholder text that shows up as hint, but not a value
- readonly makes text in input non-editable
- required makes it required that value in field before can submit
- size number of Ms that make up field (can be more entered).
Here is a form going over all the different input types.
validating forms
Attributes like minlength are used to validate forms. The form won't be submitted if not valid. Manual validation possible by using the form's onsubmit event. Returning true submits the form otherwise not submitted. Values of the form elements can be obtained using:
or if you don't have id's but have names:
Checkboxes and radio buttons have a .checked attribute to see if selected.
To see if an element meets the HTML validity requirements (minlength, maxlength, pattern, required, and type specific) use the checkValidity() method.
Dialog boxes
Dialog boxes have been faked for a long time by taking advantage of css hiding but in 2022 they were added to HTML5. Can be modal (part of the page) using .show() or modeless (float above page) using showModal().
← Return to chapter