Browse By Name
HTMLCSSJavaScriptAppendix
You are here: Reference > JavaScript > client-side > HTML DOM > objects > nodes and tags > form

form object

A A Font size Print Content Add new content Share
Browser support:
Creates a container element for controls.
The form element allows users to submit data to a server. The target URL can be specified with the action property. The method property defines the way of sending.
The contents of the message submitted to the server depend on the named form controls in the form tag. Named control means an element with a non-empty name property.
Only the contents or the state of the named form controls will be sent.
The following elements are form controls: When a form is submitted, the message contains the name/value pairs belonging to the named controls in the form. The name parameter is the value of the name property of the control. The contents of the value parameter depend on the type of the control.
Push buttons (button, input:button, input:reset, input:submit) don't play an important part in the message. If a push button is named and it is in pushed state when the form is submitted, then the name and the value (the value of the value property) of the button will be sent with the form. If a push button is not in pushed state, no information is sent with the form that belongs to the button. Since only one push button can be in pushed state at any time, they don't play an important part in the message.
The reset button can be used to reset all controls to their initial values within the form. The user can send the form to the server by clicking on the submit button.
The other controls are useful for the message submitted to the server.
If you want to check the contents of a form control before submitting, register an event handler for the onsubmit event. In your event handler you can check the required fields and if some errors are found, then you can cancel the onsubmit event (see the page for the returnValue property or the preventDefault method).
Another possibility to check the contents of a form control before submitting is to use simple push buttons instead of submit buttons. In your onclick event handler method you can check the required fields and if no error was found, you can send the form with the submit method. See the second example for details.

Syntax:

Properties that reference the object:
object.form
Methods that return the object:
document.createElement ("form")
forms.item (nameOrIndex [, subIndex])
forms.namedItem (name)
The base interface, through which you can add new functionalities to the form object, is the HTMLFormElement interface.
If you want to see the HTML objects by categories, please visit this page.
HTML page for this element: form

Possible members:

Properties
Methods
Events
Style properties
accept
Sets or retrieves a comma-separated list of accepted content types.
acceptCharset
Sets or retrieves a list of character encodings for a form.
accessKey
Sets or retrieves an access key to an element.
action
Sets or retrieves a form processing agent. With this property you can set an URL to call when the form is sent.
all
Represents a collection of all elements contained by an element or the entire document.
attributes
Represents a collection of attribute nodes that belong to an element.
autocomplete
Sets or retrieves whether the text field should give the user some suggestion during typing.
baseURI
Returns the base URL for the object.
behaviorUrns
Represents a collection of the Uniform Resource Names for all behaviors attached to an element.
canHaveChildren
Retrieves a Boolean value that indicates whether the element can contain child elements.
canHaveHTML
Retrieves a Boolean value that indicates whether the element can contain HTML formatted text.
childElementCount
Returns the number of element nodes that are direct descendants of the current element.
childNodes
Represents a collection of all nodes that are direct descendants of an element.
children
Represents a collection of all element nodes that are direct descendants of an element.
className
Sets or retrieves the style class or classes that belong to the element.
clientHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, but it does not include the scrollBar, border, and the margin.
clientLeft
Returns the width of the left border.
clientTop
Returns the height of the top border.
clientWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, but does not include the scrollBar, border, and the margin.
contentEditable
Sets or retrieves whether the contents of the object are editable.
currentStyle
Represents the computed style settings for an element.
dir
Sets or retrieves the text direction as related to the lang property.
disabled
Sets or retrieves the state of an object for user interaction.
draggable
Sets or retrieves whether an element is draggable.
elements
Represents a collection of all form control elements in a form.
encoding
Sets or retrieves how form data should be encoded for submitting to the server.
enctype
Sets or retrieves how form data should be encoded for submitting to the server.
filters
Represents a collection of all filter objects applied to an element.
firstChild
Returns a reference to the first child of the current element.
firstElementChild
Returns a reference to the first child element of the current element.
hideFocus
Specifies or returns whether a dotted rectangle (focus rectangle) is drawn around an object while it has focus.
id
Sets or retrieves a unique identifier for the object.
innerHTML
Sets or retrieves the HTML content (the source code between the start and end tag) of an object.
innerText
Sets or returns a string that represents the text content of an element.
isContentEditable
Returns a Boolean value that indicates whether the contents of the object are editable by the user.
isDisabled
Returns a Boolean value that indicates whether the object is disabled.
isMultiLine
Returns a Boolean value that indicates whether the contents of an element can be multiline or not.
isTextEdit
Returns a Boolean value that indicates whether the createTextRange method can be used for the element.
lang
Specifies or returns the language of the element.
language
Sets or retrieves the scripting language for the current element. Use it only for the script element.
lastChild
Returns a reference to the last child of the current element.
lastElementChild
Returns a reference to the last child element of the current element.
length
Returns the number of form controls in a form element or the number of options in a select or keygen element.
method
Specifies or returns how the data of a form should be submitted to the server. When the user clicks on the submit button of a form, the data of the named elements in the form will be sent. The data contains the name and the value of the named element.
name
Sets or retrieves the name of an element.
nextElementSibling
Returns a reference to the next child element of the current element's parent.
nextSibling
Returns a reference to the next child of the current element's parent.
nodeName
Returns the name of the current node.
nodeType
Returns an integer that indicates the type of the node.
nodeValue
Sets or returns the value of the current node.
offsetHeight
Returns the height of the visible area for an object, in pixels. The value contains the height with the padding, scrollBar, and the border, but does not include the margin.
offsetLeft
Returns the left position of an object relative to the left side of its offsetParent element, in pixels.
offsetParent
Returns a reference to the closest ancestor element in the DOM hierarchy from which the position of the current element is calculated.
offsetTop
Returns the top position of the object relative to the top side of its offsetParent element, in pixels.
offsetWidth
Returns the width of the visible area for an object, in pixels. The value contains the width with the padding, scrollBar, and the border, but does not include the margin.
outerHTML
Sets or retrieves the HTML content (the source code including the start and end tags) of the current element.
outerText
Sets or returns a string that represents the text content of an element.
ownerDocument
Returns the document object that contains the current element.
parentElement
Returns the parent element of the object in the DOM hierarchy.
parentNode
Returns the parent element of the object in the DOM hierarchy.
parentTextEdit
Returns the closest ancestor element of the current element in the DOM hierarchy that can be used to create a TextRange object.
previousElementSibling
Returns a reference to the previous child element of the current element's parent.
previousSibling
Returns a reference to the previous node of the current element's parent.
readyState
Returns a string value that represents the state of the object.
runtimeStyle
Represents the overridden style settings for an element.
scopeName
Retrieves the local name of the namespace declared for the current element.
scrollHeight
Returns the total height of an element's contents, in pixels. The value contains the height with the padding, but does not include the scrollBar, border, and the margin.
scrollLeft
Sets or retrieves the number of pixels by which the contents of an object are scrolled to the left.
scrollTop
Sets or retrieves the number of pixels by which the contents of an object are scrolled upward.
scrollWidth
Returns the total width of an element's contents, in pixels. The value contains the width with the padding, but does not include the scrollBar, border, and the margin.
sourceIndex
Returns the position of the current object in the all collection of the document.
spellcheck
Sets or retrieves whether the automatic spellchecker is enabled.
style
Represents the inline style settings for an element.
tabIndex
Specifies or returns the tabbing order for keyboard navigation using the TAB key.
tagName
Returns the tag name of the current element.
tagUrn
Sets or retrieves the Uniform Resource Name (URN) of the namespace declared for the current element.
target
Specifies or returns the target window or frame where the document is to be opened.
textContent
Sets or returns a string that represents the text content of an element.
title
Specifies or returns a ToolTip for an object.
uniqueID
Returns the unique identifier generated by the browser for the object.
unselectable
Sets whether an element is selectable or not.

Example HTML code 1:

This example illustrates the use of the form element:
<form action="#URL#" method="post">
    User Name: <input type="text" name="userName" />
    <br />
    Password: <input type="password" name="password" />
    <br />
    <input type="reset" value="Reset" />
    <input type="submit" value="Sign in" />
</form>
Did you find this example helpful? yes no

Example HTML code 2:

This example checks the state of some elements before submitting the form:
<head>
    <script>
        function CheckAndSubmit () {
            var userName = document.getElementById ("userName");
            if (userName.value.length < 3) {
                alert ("The name of the user must be at least 3 characters long!");
                return;
            }

            var password = document.getElementById ("password");
            var repassword = document.getElementById ("repassword");
            if (password.value.length < 6) {
                alert ("The password must be at least 6 characters long!");
                return;
            }
            if (repassword.value != password.value) {
                alert ("The two passwords are different!");
                return;
            }

            var acceptAgreement = document.getElementById ("acceptAgreement");
            if (!acceptAgreement.checked) {
                alert ("You must accept the User Agreement to register!");
                return;
            }

            var regForm = document.getElementById ("regForm");
            regForm.submit ();
        }
    </script>
</head>
<body>
    <form id="regForm" method="post" action="#URL#">
        User Name: <input type="text" name="userName" id="userName" />
        <br />
        Password: <input type="password" name="password" id="password" />
        <br />
        Repeat Password: <input type="password" name="repassword" id="repassword" />
        <br /><br />
        <input type="checkbox" name="acceptAgreement" id="acceptAgreement" /> 
        <label for="acceptAgreement">I accept the User Agreement and Privacy Policy</label>
        <br /><br />
        <input type="button" value="Register" onclick="CheckAndSubmit ()" />
    </form>
</body>
Did you find this example helpful? yes no

Related pages:

External links:

User Contributed Comments

Post Content

Ask Dottoro Post Content