Dom manipulation: forms and rich text Flashcards
What are the best ways to retrieve a form element?
document.getElementById(“formID”);
or document.forms[“formName”];
Name can be different from ID (or you can use an index number)
What special mechanism does the DOM provide that allow us to access form elements (e.g. a textarea or a button or input)?
Use someForm.elements with an index or name to retrieve a form's elements (it won't include non-input like ul or label or legend, but it will include fieldsets and not the elements inside a fieldset). var someInput = someForm.elements["input1"];
What does the form elements collection return?
A node or an HTMLCollection, depending. If a form contains several radio buttons with name=”color”, then elements[“color”] will return a collection.
How can you disable a submit button so that the form can be submitted once but not again?
form.addEventListener(“submit”, handler); Handler will set button.disabled to true.
Don’t use the click event, since this could fire before the submission actually occurs. Don’t put listener on the button itself.
What is the default type for a button element in a form?
“submit”
For non-submit buttons, change the type to “button” manually.
How can you get the text content in a text field or input field?
Use textbox.value to get and set. Don’t use textbox.getAttribute() or textbox.firstChild.nodeValue;
How can you ensure that the first element in a form gets automatically focused using HTML?
add autofocus to the HTML form element (just the word, no true or false).
What are the HTML5 alternate input types?
type=”email”, type=”url”, type=”search”, type=”tel”, type=”number” plus a number of date and time types. Note mixed browser support.
What are two html5 ways you could create an input field that allowed only multiples of 5 between 0 and 100?
/input type=”number” min=”0” max=”100” step=”5” name=”count”/ or
/input type=”range” min=”0” max=”100” step=”5”/
First lets you click up or down; second is a slider
What is an html5 way to validate that a form has a required input in which text must be either cat or dog.
use required and pattern keywords: input type=”text” required pattern=”cat|dog”
How can you make any old element editable?
set contentEditable to true. This can be done in HTML or javascript:
someDIv.contentEditable = true;
What are the two complex types of JSON syntax?
Object and Array. Array is simply: [ { “prop”: “value” } , …
What is JSON stringify() method and how does it work?
JSON.stringify(someNonJSONObject); This will serialize the object into proper JSON syntax;
What is JSON parse() method?
JSON.parse(someJSON) will turn the JSON into a javascript object.
How can you turn only certain specified properties of an object into JSON?
Use the filter argument of stringify, which is an array of properties to serialize: e.g. JSON.stringify(myObj, [“prop1”, “prop2”])