Browser: Document, Events, Interfaces
Browser: Document, Events, Interfaces
b. Backlink: element.form
c. Form elements
The environment provides its own objects and additional functions, in addition to the
basic language. Browsers, for example, provide tools for managing web pages.
Node.js makes some server-side features available, and so on.
The picture below shows in general terms what is available for JavaScript in the
browser environment:
2. Secondly, it also represents a browser window and has methods for managing it.
function sayHi() {
alert("Hello");
}
And here we are using window as the browser window object to get its height:
There are many more properties and methods for managing the browser window.
We will look at them later.
We used only document.body.style in the example, but in fact, the possibilities for
managing the page are much wider. Various properties and methods are described
in the specification: DOM Living Standard at https://dom.spec.whatwg.org.
The DOM specification describes the structure of a document and provides objects
for manipulating the page. There are other non-browser tools that use the DOM.
For example, server-side scripts that load and render HTML pages can also use the
DOM. However, they may not fully support the specification.
CSS style rules are structured differently than HTML. There is a separate CSSOM
specification for them, which explains how styles should be represented as objects,
how to read and write them.
The CSSOM is used in conjunction with the DOM when changing document styles.
In reality, CSSOM is rarely required, usually CSS rules are static. We rarely
add/remove styles from JavaScript, but it's possible.
For example:
The location object allows you to get the current URL and redirect the browser to
a new address.
The alert/confirm/prompt functions are also part of the BOM: they are not directly
related to the page, but are methods of the browser window object to communicate
with the user.
Specifications
DOM specification
Describes document structure, content manipulation, and events, see
https://dom.spec.whatwg.org for more details.
CSSOM Specification
Describes style files, rules for writing and manipulating styles, and how it all relates
to the page, see https://www.w3.org/TR/cssom-1/ for more details.
Once we've got the form, any element is available in the named form.elements
collection.
For instance:
<form name="my">
<input name="one" value="1">
<input name="two" value="2">
</form>
<script>
// getting the form
let form = document.forms.my; // <form name="my"> element
alert(elem.value); // 1
</script>
<form>
<input type="radio" name="age" value="10">
<input type="radio" name="age" value="20">
</form>
<script>
let form = document.forms[0];
These navigational properties are independent of the tag structure within the form.
All form controls, no matter how deep they are in the form, are available in the
form.elements collection.
<fieldset> as "subform"
A form can contain one or more <fieldset> elements within itself. They also support
the elements property, which holds the controls within them.
For example:
<body>
<form id="form">
<fieldset name="userFields">
<legend>info</legend>
<input name="login" type="text">
</fieldset>
</form>
<script>
alert(form.elements.login); // <input name="login">
<form id="form">
<input name="login">
</form>
<script>
alert(form.elements.login == form.login); // true, because they are the same <input>
This is usually not a problem since we rarely change the names of form elements.
Backlink: element.form
For any element, the form is available through element.form. So the form refers to all
the elements, and those elements refer to the form.
For example:
<form id="form">
<input type="text" name="login">
<script>
// form -> element
let login = form.login;
Form elements
Consider the control elements used in forms.
input и textarea
Their value can be accessed via the input.value (string) or input.checked (boolean)
property for checkboxes.
Like this:
Only the HTML that was originally on the page is stored there, not the current value.
select и option
The <select> element has 3 important properties:
The first way is the most understandable, but (2) and (3) are more convenient to use.
<select id="select">
<option value="apple">Apple</option>
<option value="pear">Pear</option>
<option value="banana">Banana</option>
</select>
<script>
// all three lines do the same thing
select.options[2].selected = true;
select.selectedIndex = 2;
select.value = 'banana';
</script>
Unlike most other controls, <select> allows us to select multiple options at the same
time if it has the multiple attribute. This feature is rarely used, but in this case, to
work with values, you must use the first method, that is, set or remove the selected
property from the <option> subelements.
<script>
// we get all selected values from select with multiple
let selected = Array.from(select.options)
.filter(option => option.selected)
.map(option => option.value);
alert(selected); // blues,rock
</script>
new Option
The spec has a nice short syntax for creating an <option> element:
There may be a little confusion with defaultSelected and selected. It's simple:
defaultSelected sets an HTML attribute, it can be obtained as
option.getAttribute('selected'), and selected - whether a value is selected or not, it is
important to set it correctly. However, usually both of these values are set to true or
not set at all (i.e. false).
Example:
To sum up
Properties for form navigation:
document.forms
form.elements
Form elements are accessible via form.elements[name/index], or you can just use
form[name/index]. The elements property also works for <fieldset>.
element.form