Dr. Greg Bernstein
Updated September 12th, 2021
<input>
elementsExample
<input type="text" id="InTest1" name="sample" value="I'm a text field">
What are the attributes?
value
: On getting, it must return the current value of the element. On setting, it must set the element’s value to the new value.name
: is used when returning info from a form or setting up a “radio button group”.Example:
<input type="password" id="InTest2" placeholder="password">
placeholder
attribute represents a short hintvalue
attribute to get the password.Live Example:
types
tel
url
, email
date
, month
, week
, time
, datetime-local
number
, range
, color
Check caniuse for current support
Text Areas:
<textarea cols="30" rows="6"
placeholder="Your stuff here">
</textarea>
Live example
Other attributes wrap
: values hard
or soft
, to prevent resizing use CSS property resize: none
.
input
event fires whenever the user has modified the data of the control.change
event fires when the value is committed, if that makes sense for the control, or else when the control loses focus. In all cases, the input event comes before the corresponding change event (if any).This script was added to the end of the slide show:
function myChange(event)
{console.log(event.target.type + " changed:" + event.target.value)}
function myInput(event)
{console.log(event.target.type + " input:" + event.target.value)}
var textArea1 = document.getElementById("TextArea1");
textArea1.addEventListener("change", myChange);
textArea1.addEventListener("input", myInput);
Use input
with type=checkbox
like so:
<input type="checkbox" checked value="Whatever" >
checked
if present makes the box checkedvalue
is convenient event in the non-form case to indicate the value of the selection.Live:
Kinda minimalistic 😦
Checkboxes are small targets we can match one with a clickable label
for easier use:
<label for="CheckTest1">Check me out 😎</label>
<input type="checkbox" id="CheckTest1" >
Live
Only one selection in a group maybe selected. To form a group we give all the radio buttons the same name
attribute:
<style>#RadioExample input {margin-right: 2em}</style>
<div id="RadioExample">
<label for="Coffee" >Coffee</label>
<input id="Coffee" type="radio" name="BevGroup" value="Coffee">
<label for="Tea" >Tea</label>
<input id="Tea" type="radio" name="BevGroup" value="Tea" checked>
<label for="Cocoa" >Cocoa</label>
<input id="Cocoa" type="radio" name="BevGroup" value="Cocoa">
</div>
Both emit input
and change
events at the same time so only need to listen for one or the other. Example code:
var coffee = document.getElementById("Coffee");
var tea = document.getElementById("Tea");
var cocoa = document.getElementById("Cocoa");
coffee.addEventListener("change", myChange);
coffee.addEventListener("input", myInput);
tea.addEventListener("change", myChange);
tea.addEventListener("input", myInput);
cocoa.addEventListener("change", myChange);
cocoa.addEventListener("input", myInput);
Two ways to specify buttons:
<button type="button">
This an <br><strong>anonymous button</strong>
</button>
<input type="button" value="This is an anonymous button">
From MDN: Buttons always behave the same whether you use a
<button>
element or an <input>
element. There are, however, some notable differences:
<button>
elements let you use HTML content in their labels, which are inserted inside the opening and closing <button>
tags.<input>
elements on the other hand are empty elements; their labels are inserted inside value
attributes, and therefore only accept plain text content.button
: anonymous button, no special behavior with forms.submit
: submits a form when clicked.reset
: resets a form to default values.Buttons generate click
events.
var myButton = document.getElementById("ClickTest1");
myButton.addEventListener("click", ()=>alert("button clicked!"));
How can that be? We see them all over…