tmt.form

tmt.form is a JavaScript library that offers some utility methods for dealing with XHTML forms. It's mostly used as an helper for TMT Validator and Ajaxform, but it can be handy on his own too.

In order to use this library you need tmt_core.js and tmt_form.js

Samples

API

tmt.form.checkFields(field1, [field2]) Check a set of form fields (radio or checkboxes).
Accepts either ids (strings) or DOM node references
tmt.form.checkGroup(groupName, values) Programmatically check radio buttons or checkboxes based on their values.
The first argument is the name of the group. The second argument can be a simple value or a comma-delimited list (for checkboxes)
tmt.form.checkSelect(theNode, values) Programmatically select options inside a <select> tag.
The first argument can be a DOM node, the id or the name of the <select>. In case of multiple tags sharing the same name the first fund in the document will be used.
The second argument can be a simple value or a comma-delimited list (for select-multiple)
tmt.form.clearField(fieldNode) Reset the value of a given form field
tmt.form.clearFields(fieldNodes) Reset the values of a given array/nodeList of form fields
tmt.form.clearForm(formNode) Reset the values of each element in a given form to blank
tmt.form.disableFields(field1, [field2]) Disable a set of form fields.
Accepts either ids (strings) or DOM node references
tmt.form.displayBox(formNode, html, cssClass) Display a message a message associated with a given form node. You can overwrite this method if you want to change the way tmt.form.displayMessage() and tmt.form.displayErrorMessage() behaves
tmt.form.displayMessage(formNode, html) Display a message a message associated with a given form node
tmt.form.displayErrorMessage(formNode, htmlCode) Display a message an error message associated with a given form node. The resulting <div> tag will have a CSS class set to "tmtFormErrorMessage"
tmt.form.enableFields(field1, [field2]) Enable a set of form fields.
Accepts either ids (strings) or DOM node references
tmt.form.getChildFields(startNode) Returns an array, containing all the form fields contained inside a given start node
Returns an empty array if no fields are available
tmt.form.getFieldGroup(fieldNode) Given a form field, return all fields contained inside the same form that share the same name.
Handy to retrieve a whole set of radio/checkboxes
tmt.form.getOptionNodeValue(optionNode) Given an option node, return its value. If no value is available, returns its text
tmt.form.getParentForm(startNode) Returns the container form node of a given node.
Returns false if the node isn't contained inside a form
tmt.form.getSubmitNodes(startNode) Returns an array of submit button nodes contained inside a given node
tmt.form.getValue(field, getGroupValue) Returns the value of a given form field.
Accepts either ids (strings) or DOM node references.
If getGroupValue argument is set to true (default to false) and fieldNode is either a radio or a checkbox; returns the value of the whole group of fields.
In order to make getGroupValue work as expected checkboxes and radio must have a "name" attribute:
Only fields considered successfull are returned
tmt.form.hashForm(formNode, demoronize) Assembles field name/value pairs from a given form, returns an object. Set the second argument to true to get rid of MS Word's special chars.
Note this method will behave exactly like a browser; it will skip fields without the name attribute, reset buttons, fieldsets and <button> elements
tmt.form.removeDisplayBox(formNode) Remove a message box (if any) associated with a given form node
tmt.form.resetGroup(groupName) Uncheck a whole group of radio buttons or checkboxes
tmt.form.resetSelect(theNode) Programmatically de-select options inside a <select> tag.
The first argument can be a DOM node, the id or the name of the <select>. In case of multiple tags sharing the same name the first fund in the document will be used.
The second argument can be a simple value or a comma-delimited list (for select-multiple)
tmt.form.selectAll(theNode) Programmatically select all options inside a select-multiple
The first argument can be a DOM node, the id or the name of the <select>
In case of multiple tags sharing the same name the first fund in the document will be used
tmt.form.serializeForm(formNode, demoronize) Assembles field name/value pairs from a given form, returns an encoded string. Set the second argument to true to get rid of MS Word's special chars.
Only fields considered successfull are returned
tmt.form.stringDemoronizer(string) Replace MS Word's non-ISO characters with plausible substitutes. Returns a "cleaned" string
tmt.form.toggleCheckFields(field1, [field2]) Toggle the checked attribute on a set of form fields.
If it's true it set it to false and viceversa. Accepts either ids (strings) or DOM node references
tmt.form.toggleEnableFields(field1, [field2]) Toggle the disabled attribute on a set of form fields. If it's true it set it to false and viceversa.
Accepts either ids (strings) or DOM node references
tmt.form.uncheckFields(field1, [field2]) Uncheck a set of form fields (radio or checkboxes).
Accepts either ids (strings) or DOM node references