Form Functions - Usage

form.js defines the following functions:

makeSearchString()

Synopsis

makeSearchString(name, value,...)

Arguments

name

A string to be associated with value. Similar to the name attribute of a form element.

value

A string to be associated with name. Similar to the value attribute of a form element.

Returns

A string of the form '?name1=value1&name2=value2'.

Description

makeSearchString() takes an even number of arguments and forms a valid query string as would be created by submitting a form using the 'get' method. Odd-numbered arguments are considered names and even-numbered ones are values. Each argument is url-encoded and pairs are joined with an =. All the sets of pairs are then concatenated using &s. Finally, a ? is prepended to the string and the string is returned.

gatherFormData()

Synopsis

gatherFormData(form)

Arguments

form

A reference to a form object.

Returns

A string of the form '?name1=value1&name2=value2' for all the elements in the form.

Description

gatherFormData() forms a valid query string as would be created by submitting the form using the 'get' method. Names and values are url-encoded and pairs are joined with an =. All the sets of pairs are then concatenated using &s. Finally, a ? is prepended to the string and the string is returned.

clearForm()

Synopsis

clearForm(form)

Arguments

form

A reference to a form object.

Returns

Nothing.

Description

Clicking a form's reset button (or invoking it's reset() method) will reset the form elements back to the values they contained when the page was loaded. If any text boxes have value attributes, or radio or checkboxes have checked attributes, or select options have selected attributes, or textareas have text between the open and close <textarea> tags, resetting a form doesn't clear it. In the case where it is preferable to clear a form, clearForm() can be used.

getSearchAsArray()

Synopsis

getSearchAsArray()

Returns

An array of all the values passed to the current page in the form of a 'get' query string indexed by their corresponding names.

Description

getSearchAsArray() gathers all the name and value pages passed to the current page in the form of a 'get' query string and makes them available in an associative array. This removes the need to parse the location.search string for the value associated with a known name. Values can be extracted from the array by using the associated name as an index to the array.

The script automatically calls this function and creates an array called query to store the name and value pairs. As an example, the value associated with the name bob is stored in query['bob'].

More about this script

Related to this script

Licensing

This script is released under a Creative Commons License.