You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Forms and control elements, such as`<input>`have a lot of special properties and events.
3
+
Form dan elemen kontrol, contohnya seperti`<input>`memiliki banyak properti khusus dan event.
4
4
5
-
Working with forms will be much more convenient when we learn them.
5
+
Bekerja dengan form akan lebih mudah ketika kita mempelajarinya.
6
6
7
-
## Navigation: form and elements
7
+
## Navigasi: form dan elemen
8
8
9
-
Document forms are members of the special collection`document.forms`.
9
+
Form dokumen adalah anggota dari koleksi khusus`document.forms`.
10
10
11
-
That's a so-called "named collection": it's both named and ordered. We can use both the name or the number in the document to get the form.
11
+
Itu disebut _"named collection"_: itu keduanya memiliki nama(name) dan terurut(index). Kita bisa menggunakan keduanya baik dengan nama atau nomor(index) pada dokumen untuk mendapatkan form.
12
12
13
13
```js no-beautify
14
14
document.forms.my- the form with name="my"
15
15
document.forms[0] - the first form in the document
16
16
```
17
17
18
-
When we have a form, then any element is available in the named collection `form.elements`.
18
+
Ketika kita mempunyai sebuah form, maka elemen apapun tersedia di dalam _named collection/koleksi nama_`form.elements`.
19
19
20
-
For instance:
20
+
Misalnya:
21
21
22
22
```html run height=40
23
23
<formname="my">
24
-
<inputname="one"value="1">
25
-
<inputname="two"value="2">
24
+
<inputname="one"value="1" />
25
+
<inputname="two"value="2" />
26
26
</form>
27
27
28
28
<script>
29
-
//get the form
30
-
let form =document.forms.my; // <form name="my"> element
29
+
//dapatkan form
30
+
let form =document.forms.my; // <form name="my"> elemen
31
31
32
-
//get the element
33
-
let elem =form.elements.one; // <input name="one"> element
32
+
//dapatkan element
33
+
let elem =form.elements.one; // <input name="one"> elemen
34
34
35
35
alert(elem.value); // 1
36
36
</script>
37
37
```
38
38
39
-
There may be multiple elements with the same name, that's often the case with radio buttons.
39
+
Ada suatu saat dimana ada beberapa elemen dengan nama yang sama, hal itu sering terjadi dengan _radio buttons_.
40
40
41
-
In that case`form.elements[name]` is a collection, for instance:
41
+
Dalam hal tersebut`form.elements[name]`adalah sebuah _collection/koleksi_, misalnya:
These navigation properties do not depend on the tag structure. All control elements, no matter how deep they are in the form, are available in`form.elements`.
60
+
Navigasi properti ini tidak bergantung pada struktur tag. Semua elemen kontrol, tak peduli seberapa dalam mereka didalam form, mereka tersedia di dalam`form.elements`.
61
61
62
+
````smart header="Fieldsets sebagai "subforms""
63
+
Sebuah form mungkin punya satu atau banyak elemen `<fieldset>` didalamnya. Mereka juga punya `elements` properti yang mencatumkan form kontrol didalamnya.
62
64
63
-
````smart header="Fieldsets as \"subforms\""
64
-
A form may have one or many `<fieldset>` elements inside it. They also have `elements` property that lists form controls inside them.
65
-
66
-
For instance:
65
+
Misalnya:
67
66
68
67
```html run height=80
69
68
<body>
@@ -81,57 +80,57 @@ For instance:
81
80
let fieldset = form.elements.userFields;
82
81
alert(fieldset); // HTMLFieldSetElement
83
82
84
-
// we can get the input by name both from the form and from the fieldset
83
+
// kita bisa mendapatkan input dengan nama baik dari form maupun dari fieldset
There's a shorter notation: we can access the element as `form[index/name]`.
91
+
````warn header="Shorter notation: `form.name`" Ada notasi yang lebih ringkas: kita bisa akses/mendapatkan elemen dengan `form[index/name]`.
94
92
95
-
In other words, instead of `form.elements.login`we can write`form.login`.
93
+
Dengan kata lain, daripada menulisnya dengan `form.elements.login`kita bisa menulis`form.login`.
96
94
97
-
That also works, but there's a minor issue: if we access an element, and then change its `name`, then it is still available under the old name (as well as under the new one).
95
+
Itu juga berkeja, tetapi disana ada sebuah kesalahan kecil: jika kita akses sebuah elemen, dan lalu mengubah `name`, maka elemen tersebut masih tersedia dengan nama lamanya (sama juga dengan nama barunya).
98
96
99
-
That's easy to see in an example:
97
+
Itu akan lebih mudah saat kita lihat pada sebuah contoh:
100
98
101
99
```html run height=40
102
100
<formid="form">
103
-
<inputname="login">
101
+
<inputname="login" />
104
102
</form>
105
103
106
104
<script>
107
-
alert(form.elements.login==form.login); // true, the same <input>
105
+
alert(form.elements.login==form.login); // true, <input> yang sama
108
106
109
-
form.login.name="username"; //change the name of the input
// form allows both names: the new one and the old one
117
-
alert(form.username==form.login); // true
118
-
*/!*
113
+
*!*
114
+
// form membolehkan kedua nama: yang baru dan yang lama
115
+
alert(form.username==form.login); // true
116
+
*/!*
119
117
</script>
120
118
```
121
119
122
-
That's usually not a problem, because we rarely change names of form elements.
120
+
Itu biasanya bukan sebuah masalah, karena kita jarang mengubah nama dari elemen form.
123
121
124
122
````
125
123
126
124
## Backreference: element.form
127
125
128
-
For any element, the form is available as `element.form`. So a form references all elements, and elements reference the form.
126
+
Untuk elemen apapun, form tersedia sebagai `element.form`. Jadi sebuah form mereferensikan semua elemen, dan elemen referensi dari form.
129
127
130
-
Here's the picture:
128
+
129
+
Ini gambarannya:
131
130
132
131

133
132
134
-
For instance:
133
+
Misalnya:
135
134
136
135
```html run height=40
137
136
<form id="form">
@@ -140,55 +139,55 @@ For instance:
140
139
141
140
<script>
142
141
*!*
143
-
// form -> element
142
+
// form -> elemen
144
143
let login = form.login;
145
144
146
-
// element -> form
145
+
// elemen -> form
147
146
alert(login.form); // HTMLFormElement
148
147
*/!*
149
148
</script>
150
149
```
151
150
152
-
## Form elements
151
+
## Element Form
153
152
154
-
Let's talk about form controls.
153
+
Mari bicara tentang kontrol form.
155
154
156
-
### input and textarea
155
+
### input dan textarea
157
156
158
-
We can access their value as `input.value` (string) or `input.checked` (boolean) for checkboxes.
157
+
Kita bisa akses nilai mereka dengan `input.value` (string) atau `input.checked` (boolean) untuk checkboxes.
159
158
160
-
Like this:
159
+
Seperti ini:
161
160
162
161
```js
163
162
input.value = "New value";
164
163
textarea.value = "New text";
165
164
166
-
input.checked = true; // for a checkbox or radio button
165
+
input.checked = true; // untuk checkbox atau radio button
167
166
```
168
167
169
168
```warn header="Use `textarea.value`, not `textarea.innerHTML`"
170
-
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML` to access it.
169
+
Harap dicatat, meskipun `<textarea>...</textarea>` menyimpan nilainya sebagai HTML bersarang(nested), kita tidak boleh menggunakan `textarea.innerHTML` untuk mengaksesnya.
171
170
172
-
It stores only the HTML that was initially on the page, not the current value.
171
+
itu hanya menyimpan HTML yang mulanya ada di halaman, bukan nilai saat ini.
173
172
```
174
173
175
-
### select and option
174
+
### select dan option
176
175
177
-
A `<select>` element has 3 important properties:
176
+
Sebuah elemen `<select>` mempunyai 3 properti penting:
178
177
179
-
1. `select.options` -- the collection of `<option>` subelements,
180
-
2. `select.value` -- the value of the currently selected `<option>`,
181
-
3. `select.selectedIndex` -- the number of the currently selected `<option>`.
178
+
1. `select.options` -- adalah koleksi dari `<option>` sub-element,
179
+
2. `select.value` -- adalah nilai saat ini yang dipilih(selected) `<option>`,
180
+
3. `select.selectedIndex` -- adalah nomor yang saat ini dipilih(selected) `<option>`.
182
181
183
-
They provide three different ways of setting a value for a `<select>`:
182
+
Mereka menyediakan 3 cara berbeda untuk mengatur nilai pada `<select>`:
184
183
185
-
1. Find the corresponding `<option>` element and set `option.selected` to `true`.
186
-
2. Set `select.value` to the value.
187
-
3. Set `select.selectedIndex` to the number of the option.
184
+
1. Mencari element `<option>` yang bersangkutan dan atur `option.selected` menjadi `true`.
185
+
2. Atur `select.value`ke nilai.
186
+
3. Atur `select.selectedIndex` ke nomor dari option.
188
187
189
-
The first way is the most obvious, but `(2)` and `(3)` are usually more convenient.
188
+
Cara pertama adalah yang paling jelas, tetapi cara `(2)` dan `(3)` biasanya lebih nyaman.
190
189
191
-
Here is an example:
190
+
Lihat contoh berikut:
192
191
193
192
```html run
194
193
<select id="select">
@@ -198,18 +197,18 @@ Here is an example:
198
197
</select>
199
198
200
199
<script>
201
-
// all three lines do the same thing
200
+
// semua 3 baris kode melakukan hal yang sama
202
201
select.options[2].selected = true;
203
202
select.selectedIndex = 2;
204
203
select.value = 'banana';
205
204
</script>
206
205
```
207
206
208
-
Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. Although such functionality is available, it is rarely used.
207
+
Tidak seperti kontrol pada umumnya, `<select>` membolehkan memilih banyak opsi sekaligus jika memiliki atribut`multiple`.Fitur itu jarang digunakan.
209
208
210
-
In cases that you have to, then use the first way: add/remove the `selected` property from `<option>` subelements.
209
+
Jika anda harus, maka gunakan cara pertama: tambah/hapus `selected`properti dari `<option>` sub-element.
211
210
212
-
We can get their collection as `select.options`, for instance:
211
+
Kita bisa mendapatkan koleksinya sebagai `select.options`, misalnya:
213
212
214
213
```html run
215
214
<select id="select" *!*multiple*/!*>
@@ -219,81 +218,82 @@ We can get their collection as `select.options`, for instance:
219
218
</select>
220
219
221
220
<script>
222
-
// get all selected values from multi-select
221
+
// mendapatkan semua nilai selected dari multi-select
223
222
let selected = Array.from(select.options)
224
223
.filter(option => option.selected)
225
224
.map(option => option.value);
226
225
227
-
alert(selected); // blues,rock
226
+
alert(selected); // blues,rock
228
227
</script>
229
228
```
230
229
231
-
The full specification of the `<select>` element is available in the specification <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
230
+
Penjelasan lengkap dari elemen`<select>` tersedia di <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
232
231
233
232
### new Option
234
233
235
-
This is rarely used on its own. But there's still an interesting thing.
234
+
Ini jarang digunakan. Namun masih ada hal yang menarik.
236
235
237
-
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
236
+
Di dalam [penjelasan](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) disana ada sintak pendek yang bagus untuk membuat elemen `<option>`:
238
237
239
238
```js
240
239
option = new Option(text, value, defaultSelected, selected);
241
240
```
242
241
243
242
Parameters:
244
243
245
-
- `text` -- the text inside the option,
246
-
- `value` -- the option value,
247
-
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
248
-
- `selected` -- if `true`, then the option is selected.
244
+
- `text` -- adalah teks didalam option,
245
+
- `value` -- adalah nilai option,
246
+
- `defaultSelected` -- jika `true`, maka `selected` HTML-attribute dibuat,
247
+
- `selected` -- jika `true`, maka option nya *selected*.
249
248
250
-
There may be a small confusion about `defaultSelected` and `selected`. That's simple: `defaultSelected` sets HTML-attribute, that we can get using `option.getAttribute('selected')`. And `selected` - whether the option is selected or not, that's more important. Usually both values are either set to `true` or not set (same as `false`).
249
+
Disana mungkin sedikit bingung tentang `defaultSelected` dan `selected`. That's simple: `defaultSelected` *set* HTML-attribute, dengan itu kita bisa dapat menggunakan `option.getAttribute('selected')`. Dan `selected` - baik opsi *selected* atau tidak, itu yang lebih penting. Biasanya kedua nilai baik di *set* ke `true` atau tidak di *set* (sama dengan `false`).
251
250
252
-
For instance:
251
+
Misalnya:
253
252
254
253
```js
255
254
let option = new Option("Text", "value");
256
255
// creates <option value="value">Text</option>
257
256
```
258
257
259
-
The same element selected:
258
+
Elemen yang sama yang terpilih/*selected*:
260
259
261
260
```js
262
261
let option = new Option("Text", "value", true, true);
263
262
```
264
263
265
-
Option elements have properties:
264
+
Elemen *option* memiliki properti:
266
265
267
266
`option.selected`
268
-
: Is the option selected.
267
+
: apakah option *selected*.
269
268
270
269
`option.index`
271
-
: The number of the option among the others in its `<select>`.
270
+
: Jumlah option diantara yang lain dalam elemen`<select>`.
272
271
273
272
`option.text`
274
-
: Text content of the option (seen by the visitor).
: A form is available as `document.forms[name/index]`.
284
+
: Sebuah form tersedia sebagai `document.forms[name/index]`.
286
285
287
-
`form.elements`
288
-
: Form elements are available as `form.elements[name/index]`, or can use just `form[name/index]`. The `elements` property also works for `<fieldset>`.
286
+
`form.elements`
287
+
: Elemen form tersedia sebagai `form.elements[name/index]`, atau kita bisa gunakan `form[name/index]`. `elements` properti juga dapat berkerja dengan `<fieldset>`.
289
288
290
289
`element.form`
291
-
: Elements reference their form in the `form` property.
290
+
: Elemen referensi formulirnya dalam `form` properti.
292
291
293
-
Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
292
+
Nilai tersedia sebagai `input.value`, `textarea.value`, `select.value` dll, atau`input.checked` untuk *checkboxes* dan *radio buttons*.
294
293
295
-
For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
294
+
Untuk `<select>` kita juga bisa mendapatkan nilainya dengan index `select.selectedIndex` atau lewat koleksi option `select.options`.
296
295
297
-
These are the basics to start working with forms. We'll meet many examples further in the tutorial.
296
+
Ini adalah dasar-dasar untuk mulai bekerja dengan form. Kita akan melihat banyak contoh lebih lanjut di tutorial.
298
297
299
-
In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
298
+
Pada bab selanjutnya, kita akan membahas `focus` dan `blur` event yang mungkin terjadi pada elemen manapapun, tapi biasanya ditangani pada form.
0 commit comments