Mike's JavaScript Crib Sheet
String Methods/Properties:
value can be compared to "" for an empty input
note that spaces aren't considered to be empty
- indexOf( CharToFind, IndexOfStartingChar)
- returns the index of the first CharToFind that occurs in the
string after IndexOfStartingChar, or "-1" if it can't find
any more
- charAt( Index )
- Returns the character at Index in the string
- length Property
- Number of characters in the string
Function Args:
- FunctionName[i]
- fetches the ith argument to the function FunctionName
Useful for iterating through variable-arg functions
- FunctionName.length Property
- Returns the number of arguments to the function FunctionName
Using JS to check form inputs
-
Use the "onClick" event handler to invoke methods that check the values
of strings in various form elements, which is good for
interactive checking (eg, computing a GPA and returning it to the
user)
- Use the "onSubmit" event handler in a <FORM> tag to
check the input if the onSubmit handler returns "true",
then the form is submitted. If the handler returns "false", it isn't
- Use the "onLoad" event handler in a ≶BODY> tag to
execute code as soon as the page is loaded (for example, setting the focus)
Form Element Methods/Properties
- focus()
- sets the focus to be the form element
- select()
- Selects the text in a text form element