More events than usual
Inputs
Inputs are another way to get responses from users. These are the ‘form elements’ one is used to seeing all over the web.
Some major ones:
- Text fields
- Buttons
- Selects
Some other, fun ones (that we may or may not cover in class):
- Date pickers
- Sliders
Accessing input values in javascript
Using the value property
Works with selects and inputs, our two major players.
Buttons
- Are, of course, good for clicking
- Which makes them really good for… mouse events.
Validation
One nice way to use Javascript and forms is to validate user inputs.
Forms
Before we can validate, we need to know about the form tag. The form tag is a tag we can wrap inputs with that both can be used to submit data. If we give this tag an id, we can explicitly dissallow the form from being submitted if the user entered bad inputs.
Listening for a form submission
Stopping an event from doing what is usually does
Many events do something by default (click events on links will navigate to urls, submmitting a form will move to the next page)
We can, however, choose to ‘interrupt’ that event and stop it from doing what it would normally do with:
Validating if things are textual or numeric
isNaN: returns true if something is not a number
Validating length
Use the .length property of a string
The start of form validation
Sound
Getting sound into our web pages isn’t too hard, use the audio tag
Note that there can be more than one file per audio element. This is because there is no single format supported by all browsers.
Playing a sound
Listening for sound events
There’s more to life than mouse events. Let’s listen for when a sound ends: