JavaScript

Forms and Controls

Forms <form> and controls: <input>, <select>, etc


Form properties and methods

Navigate forms

document.forms.formName
document.forms[0]

Navigate elements

formName.elements.elementName
formName.elements[0]

Document forms are members of the special collection document.forms.

Named collection: it's both named and ordered. We can use both name or number in the document to get the form.

<form name="zoo">
    <input name="cat" value="Kitten">
    <input name="dog" value="Doggie">
</form>
<p>catElem.value = <span id="result1"></span></p>
<p>dogElem.value = <span id="result2"></span></p>

<script>
    let zooForm = document.forms.zoo;
    let catElem = zooForm.elements.cat;
    let dogElem = zooForm.elements.dog;
    document.querySelector("#result1").textContent = catElem.value;
    document.querySelector("#result2").textContent = dogElem.value;
</script>

catElem.value =

dogElem.value =

Navigate a collection of elements with the same name

formName.elements.elementName[0]
formName.elements[0]

It's possible to have multiple elements with one same name. This is typical for radio buttons and checkboxes. In this case, formName.elements.elementName is a collection and can be referred individually using their index (the order of the element being listed).


Explanations:


Form learning put in halt, moving on to object.

Main resources: