Skip to content

Form properties and methods #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
503f8e8
2-ui/4-forms-controls/1-form-elements/article.md updated from https:/…
Pazmy Oct 18, 2020
8a05270
2-ui/4-forms-controls/1-form-elements/article.md updated from https:/…
Pazmy Oct 18, 2020
23da5a7
2-ui/4-forms-controls/1-form-elements/article.md updated from https:/…
Pazmy Oct 18, 2020
282f558
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
9077537
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
779262c
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
90f2a71
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
99ba465
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
6f1bd59
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
08fed1c
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
a53e4fb
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
a345edb
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
7700cb8
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
a745718
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 19, 2020
999b30a
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
a80626f
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
e824ecc
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
2c96da1
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
5f259c5
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
48f3645
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
b6e337d
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
f9235b2
2-ui/4-forms-controls/2-focus-blur/article.md updated from https://st…
Pazmy Oct 20, 2020
b2c88c8
2-ui/4-forms-controls/2-focus-blur/3-editable-div/task.md updated fro…
Pazmy Oct 20, 2020
afb8c01
2-ui/4-forms-controls/2-focus-blur/3-editable-div/task.md updated fro…
Pazmy Oct 20, 2020
4642ab7
2-ui/4-forms-controls/2-focus-blur/3-editable-div/task.md updated fro…
Pazmy Oct 20, 2020
94b68be
2-ui/4-forms-controls/2-focus-blur/3-editable-div/task.md updated fro…
Pazmy Oct 20, 2020
40fff5b
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md updated fr…
Pazmy Oct 20, 2020
58d1d78
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md updated fr…
Pazmy Oct 20, 2020
8d27b09
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md updated fr…
Pazmy Oct 20, 2020
65aeba8
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md updated fr…
Pazmy Oct 20, 2020
d238634
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md updated fr…
Pazmy Oct 20, 2020
1a29f59
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md updated fr…
Pazmy Oct 20, 2020
6902fa5
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/solution.md update…
Pazmy Oct 20, 2020
619d4c8
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/solution.md update…
Pazmy Oct 20, 2020
79e7d27
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/solution.md update…
Pazmy Oct 20, 2020
4b8249e
2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/solution.md update…
Pazmy Oct 20, 2020
479e02e
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/task.md updated f…
Pazmy Oct 20, 2020
496b4b8
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/task.md updated f…
Pazmy Oct 20, 2020
1c83acc
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/task.md updated f…
Pazmy Oct 20, 2020
e10856b
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md updat…
Pazmy Oct 20, 2020
5af493e
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md updat…
Pazmy Oct 20, 2020
d3c307c
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md updat…
Pazmy Oct 20, 2020
2352138
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md updat…
Pazmy Oct 20, 2020
fafe752
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md updat…
Pazmy Oct 20, 2020
a096457
2-ui/4-forms-controls/2-focus-blur/5-keyboard-mouse/solution.md updat…
Pazmy Oct 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
180 changes: 92 additions & 88 deletions 2-ui/4-forms-controls/1-form-elements/article.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Form properties and methods
# Metode dan properti form

Forms and control elements, such as `<input>` have a lot of special properties and events.
Form dan elemen control, contohnya seperti `<input>` memiliki banyak properti khusus dan event.

Working with forms will be much more convenient when we learn them.
Bekerja dengan form akan lebih mudah ketika kita mempelajarinya.

## Navigation: form and elements
## Navigasi: form dan elemen

Document forms are members of the special collection `document.forms`.
Form dokumen adalah anggota dari koleksi khusus `document.forms`.

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.
Itu disebut _"named collection"_: itu keduanya memiliki nama(name) dan terurut(index). Kita bisa menggunakan keduanya baik dengan nama atau nomor pada dokumen untuk mendapatkan form .

```js no-beautify
document.forms.my - the form with name="my"
document.forms[0] - the first form in the document
```

When we have a form, then any element is available in the named collection `form.elements`.
Ketika kita mempunyai sebuah form, maka elemen apapun tersedia di dalam _named collection/koleksi nama_ `form.elements`.

For instance:
Misalnya:

```html run height=40
<form name="my">
Expand All @@ -26,19 +26,20 @@ For instance:
</form>

<script>
// get the form
let form = document.forms.my; // <form name="my"> element
// dapatkan form
let form = document.forms.my; // <form name="my"> elemen

// get the element
let elem = form.elements.one; // <input name="one"> element
// dapatkan element
let elem = form.elements.one; // <input name="one"> elemen

alert(elem.value); // 1
</script>
```

There may be multiple elements with the same name, that's often the case with radio buttons.
Ada suatu saat dimana ada beberapa elemen dengan nama yang sama, hal itu sering terjadi dengan _radio buttons_.

In that case `form.elements[name]` is a collection, for instance:

Dalam hal tersebut `form.elements[name]`adalah sebuah _collection/koleksi_, misalnya:

```html run height=40
<form>
Expand All @@ -57,13 +58,13 @@ alert(ageElems[0]); // [object HTMLInputElement]
</script>
```

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`.
Navigasi properti ini tidak bergantung pada struktur tag. Semua elemen kontrol, tak peduli seberapa dalam mereka didalam form, mereka tersedia di dalam `form.elements`.


````smart header="Fieldsets as \"subforms\""
A form may have one or many `<fieldset>` elements inside it. They also have `elements` property that lists form controls inside them.
````smart header="Fieldsets sebagai "subforms""
Sebuah form mungkin punya satu atau banyak elemen `<fieldset>` didalamnya. Mereka juga punya `elements` properti yang mencatumkan form kontrol didalamnya.

For instance:
Misalnya:

```html run height=80
<body>
Expand All @@ -81,57 +82,57 @@ For instance:
let fieldset = form.elements.userFields;
alert(fieldset); // HTMLFieldSetElement

// we can get the input by name both from the form and from the fieldset
// kita bisa mendapatkan input dengan nama baik dari form maupun dari fieldset
alert(fieldset.elements.login == form.elements.login); // true
*/!*
</script>
</body>
```
````

````warn header="Shorter notation: `form.name`"
There's a shorter notation: we can access the element as `form[index/name]`.
````warn header="Shorter notation: `form.name`" Ada notasi yang lebih ringkas: kita bisa akses/mendapatkan elemen dengan `form[index/name]`.

In other words, instead of `form.elements.login` we can write `form.login`.
Dengan kata lain, daripada menulisnya dengan `form.elements.login` kita bisa menulis `form.login`.

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).
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).

That's easy to see in an example:
Itu akan lebih mudah saat kita lihat pada sebuah contoh:

```html run height=40
<form id="form">
<input name="login">
</form>

<script>
alert(form.elements.login == form.login); // true, the same <input>
alert(form.elements.login == form.login); // true, <input> yang sama

form.login.name = "username"; // change the name of the input
form.login.name = "username"; // mengubah nama input

// form.elements updated the name:
alert(form.elements.login); // undefined
alert(form.elements.username); // input
// form.elements telah mengupdate nama:
alert(form.elements.login); // undefined (tidak terdefinisi)
alert(form.elements.username); // input

*!*
// form allows both names: the new one and the old one
alert(form.username == form.login); // true
*/!*
*!*
// form membolehkan kedua nama: yang baru dan yang lama
alert(form.username == form.login); // true
*/!*
</script>
```

That's usually not a problem, because we rarely change names of form elements.
Itu biasanya bukan sebuah masalah, karena kita jarang mengubah nama dari elemen form.

````

## Backreference: element.form

For any element, the form is available as `element.form`. So a form references all elements, and elements reference the form.
Untuk elemen apapun, form tersedia sebagai `element.form`. Jadi sebuah form mereferensikan semua elemen, dan elemen referensi dari form.


Here's the picture:
Ini gambarannya:

![](form-navigation.svg)

For instance:
Misalnya:

```html run height=40
<form id="form">
Expand All @@ -140,55 +141,55 @@ For instance:

<script>
*!*
// form -> element
// form -> elemen
let login = form.login;

// element -> form
// elemen -> form
alert(login.form); // HTMLFormElement
*/!*
</script>
```

## Form elements
## Element Form

Let's talk about form controls.
Mari bicara tentang kontrol form.

### input and textarea
### input dan textarea

We can access their value as `input.value` (string) or `input.checked` (boolean) for checkboxes.
Kita bisa akses nilai mereka dengan `input.value` (string) atau `input.checked` (boolean) untuk checkboxes.

Like this:
Seperti ini:

```js
input.value = "New value";
textarea.value = "New text";

input.checked = true; // for a checkbox or radio button
input.checked = true; // untuk checkbox atau radio button
```

```warn header="Use `textarea.value`, not `textarea.innerHTML`"
Please note that even though `<textarea>...</textarea>` holds its value as nested HTML, we should never use `textarea.innerHTML` to access it.
Harap dicatat, meskipun `<textarea>...</textarea>` menyimpan nilainya sebagai HTML bersarang(nested), kita tidak boleh menggunakan `textarea.innerHTML` untuk mengaksesnya.

It stores only the HTML that was initially on the page, not the current value.
itu hanya menyimpan HTML yang mulanya ada di halaman, bukan nilai saat ini.
```

### select and option
### select dan option

A `<select>` element has 3 important properties:
Sebuah elemen `<select>` mempunyai 3 properti penting:

1. `select.options` -- the collection of `<option>` subelements,
2. `select.value` -- the value of the currently selected `<option>`,
3. `select.selectedIndex` -- the number of the currently selected `<option>`.
1. `select.options` -- adalah koleksi dari `<option>` sub-element,
2. `select.value` -- adalah nilai saat ini yang dipilih(selected) `<option>`,
3. `select.selectedIndex` -- adalah nomor yang saat ini dipilih(selected) `<option>`.

They provide three different ways of setting a value for a `<select>`:
Mereka menyediakan 3 cara berbeda untuk mengatur nilai pada `<select>`:

1. Find the corresponding `<option>` element and set `option.selected` to `true`.
2. Set `select.value` to the value.
3. Set `select.selectedIndex` to the number of the option.
1. Mencari element `<option>` yang bersangkutan dan atur `option.selected` menjadi `true`.
2. Atur `select.value`ke nilai.
3. Atur `select.selectedIndex` ke nomor dari option.

The first way is the most obvious, but `(2)` and `(3)` are usually more convenient.
Cara pertama yang paling jelas, tetapi cara `(2)` dan `(3)` biasanya lebih nyaman.

Here is an example:
Lihat contoh berikut:

```html run
<select id="select">
Expand All @@ -198,16 +199,16 @@ Here is an example:
</select>

<script>
// all three lines do the same thing
// semua 3 baris kode melakukan hal yang sama
select.options[2].selected = true;
select.selectedIndex = 2;
select.value = 'banana';
</script>
```

Unlike most other controls, `<select>` allows to select multiple options at once if it has `multiple` attribute. That's feature is rarely used. In that case we need to use the first way: add/remove the `selected` property from `<option>` subelements.
Tidak seperti kontrol pada umumnya, `<select>` membolehkan memilih banyak opsi sekaligus jika memiliki atribut`multiple`.Fitur itu jarang digunakan. Dalam hal itu kita perlu menggunakan cara pertama: tambah/hapus `selected`properti dari `<option>` sub-element.

We can get their collection as `select.options`, for instance:
Kita bisa mendapatkan koleksinya sebagai `select.options`, misalnya:

```html run
<select id="select" *!*multiple*/!*>
Expand All @@ -217,7 +218,7 @@ We can get their collection as `select.options`, for instance:
</select>

<script>
// get all selected values from multi-select
// mendapatkan semua nilai selected dari multi-select
let selected = Array.from(select.options)
.filter(option => option.selected)
.map(option => option.value);
Expand All @@ -226,72 +227,75 @@ We can get their collection as `select.options`, for instance:
</script>
```

The full specification of the `<select>` element is available in the specification <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
Penjelasan lengkap dari elemen`<select>` tersedia di <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.

### new Option

This is rarely used on its own. But there's still an interesting thing.
Ini jarang digunakan. Namun masih ada hal yang menarik.

In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
Di dalam [penjelasan](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) disana ada sintak pendek yang bagus untuk membuat elemen `<option>`:

```js
option = new Option(text, value, defaultSelected, selected);
```

Parameters:

- `text` -- the text inside the option,
- `value` -- the option value,
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
- `selected` -- if `true`, then the option is selected.
- `text` -- adalah teks didalam option,
- `value` -- adalah nilai option,
- `defaultSelected` -- jika `true`, maka `selected` HTML-attribute dibuat,
- `selected` -- jika `true`, maka option nya *selected*.

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`).
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`).

For instance:
Misalnya:

```js
let option = new Option("Text", "value");
// creates <option value="value">Text</option>
```

The same element selected:
Elemen yang sama yang terpilih/*selected*:

```js
let option = new Option("Text", "value", true, true);
```

Option elements have properties:
Elemen *option* memiliki properti:

`option.selected`
: Is the option selected.
: apakah option *selected*.

`option.index`
: The number of the option among the others in its `<select>`.
: Jumlah option diantara yang lain dalam elemen`<select>`.

`option.text`
: Text content of the option (seen by the visitor).
: Konten teks option(dilihat oleh pengunjung).

## References
## Referensi

- Specification: <https://html.spec.whatwg.org/multipage/forms.html>.
- Spesifikasi: <https://html.spec.whatwg.org/multipage/forms.html>.

## Summary
## Kesimpulan

Form navigation:
Navigasi Form:

`document.forms`
: A form is available as `document.forms[name/index]`.
: Sebuah form tersedia sebagai `document.forms[name/index]`.

`form.elements`
: Form elements are available as `form.elements[name/index]`, or can use just `form[name/index]`. The `elements` property also works for `<fieldset>`.
`form.elements`
: Elemen form tersedia sebagai `form.elements[name/index]`, atau kita bisa gunakan `form[name/index]`. `elements` properti juga dapat berkerja dengan `<fieldset>`.

`element.form`
: Elements reference their form in the `form` property.
: Elemen referensi formulirnya dalam `form` properti.

Value is available as `input.value`, `textarea.value`, `select.value` etc, or `input.checked` for checkboxes and radio buttons.
Nilai tersedia sebagai `input.value`, `textarea.value`, `select.value` etc, atau`input.checked` untuk *checkboxes* dan *radio buttons*.

For `<select>` we can also get the value by the index `select.selectedIndex` or through the options collection `select.options`.
Untuk `<select>` kita juga bisa mendapatkan nilainya dengan index `select.selectedIndex` atau lewat koleksi option `select.options`.

These are the basics to start working with forms. We'll meet many examples further in the tutorial.
Ini adalah dasar-dasar untuk mulai bekerja dengan form. Kita akan melihat banyak contoh lebih lanjut di tutorial.

In the next chapter we'll cover `focus` and `blur` events that may occur on any element, but are mostly handled on forms.
Pada bab selanjutnya, kita akan membahas `focus` dan `blur` event yang mungkin terjadi pada elemen apapun, tapi biasanya ditangani pada form.
<!--stackedit_data:
eyJoaXN0b3J5IjpbLTUwNDcwMzkzNCwtMTkwMjI1NTExMF19
-->
9 changes: 6 additions & 3 deletions 2-ui/4-forms-controls/2-focus-blur/3-editable-div/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ importance: 5

# Editable div

Create a `<div>` that turns into `<textarea>` when clicked.
Buat sebuah `<div>` yang berubah menjadi`<textarea>` ketika di klik.

The textarea allows to edit the HTML in the `<div>`.
Textarea memungkinkan untuk mengedit HTML pada `<div>`.

When the user presses `key:Enter` or it loses focus, the `<textarea>` turns back into `<div>`, and its content becomes HTML in `<div>`.
Ketika user menekan `key:Enter` atau hilang fokus, maka `<textarea>` berubah kembali menjadi `<div>`, dan isinya menjadi HTML di dalam `<div>`.

[demo src="solution"]
<!--stackedit_data:
eyJoaXN0b3J5IjpbLTE0MTc4NzkyMDNdfQ==
-->
11 changes: 7 additions & 4 deletions 2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

1. On click -- replace `innerHTML` of the cell by `<textarea>` with same sizes and no border. Can use JavaScript or CSS to set the right size.
2. Set `textarea.value` to `td.innerHTML`.
3. Focus on the textarea.
4. Show buttons OK/CANCEL under the cell, handle clicks on them.
1. Saat diklik -- ganti `innerHTML` dari sel oleh `<textarea>`dengan ukuran yang sama dan tanpa border.Bisa menggunakan JavaScript atau CSS untuk mengatur ukuran yang sesuai.
2. Set `textarea.value` ke `td.innerHTML`.
3. Fokus pada textarea.
4. Tampilkan tombol OK/CANCEL dibawah sel, handle klik pada mereka.
<!--stackedit_data:
eyJoaXN0b3J5IjpbMTg5NzE4OTY4OF19
-->
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy