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
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
+
폼 요소 탐색에 쓰이는 프로퍼티는 태그 구조에 의존하지 않습니다. 폼을 조작하는 데 쓰이는 요소들은 모두 태그 깊이에 상관없이 `form.elements`을 사용해 접근할 수 있습니다.
61
61
62
62
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.
63
+
````smart header="\'하위 폼\'처럼 쓰이는 fieldset"
64
+
폼은 하나 이상의 `<fieldset>` 요소를 포함할 수 있습니다. 폼 내부에 있는 fieldset도 폼과 마찬가지로 자신의 내부에 있는 폼 조작 요소에 접근할 수 있도록 해주는 `elements` 프로퍼티를 지원합니다.
65
65
66
-
For instance:
66
+
예시:
67
67
68
68
```html run height=80
69
69
<body>
@@ -81,57 +81,57 @@ For instance:
81
81
let fieldset = form.elements.userFields;
82
82
alert(fieldset); // HTMLFieldSetElement
83
83
84
-
// we can get the input by name both from the form and from the fieldset
There's a shorter notation: we can access the element as `form[index/name]`.
92
+
````warn header="짧은 표기법: `form.name`"
93
+
짧은 표기법인 `form[index/name]`으로도 요소에 접근할 수 있습니다.
94
94
95
-
In other words, instead of `form.elements.login`we can write `form.login`.
95
+
`form.elements.login`대신 `form.login`처럼 쓸 수 있죠.
96
96
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).
97
+
`form.name`을 사용한 표기법은 잘 작동하긴 하지만 요소에 접근해 `name` 속성을 변경해도 변경 전 이름을 계속 사용할 수 있다는 문제가 있습니다(물론 새로운 이름도 사용할 수 있습니다).
98
98
99
-
That's easy to see in an example:
99
+
예시를 통해 확인해 봅시다.
100
100
101
101
```html run height=40
102
102
<formid="form">
103
103
<inputname="login">
104
104
</form>
105
105
106
106
<script>
107
-
alert(form.elements.login==form.login); // true, the same <input>
107
+
alert(form.elements.login==form.login); // true, 동일한 <input>입니다.
108
108
109
-
form.login.name="username"; //change the name of the input
109
+
form.login.name="username"; // input의 name 속성을 변경합니다.
110
110
111
-
// form.elements updated the name:
111
+
// form.elements에는 name 속성 변경이 반영되었습니다.
112
112
alert(form.elements.login); // undefined
113
113
alert(form.elements.username); // input
114
114
115
115
*!*
116
-
//form allows both names: the new one and the old one
116
+
//form은 새로운 이름과 이전 이름을 모두 인식합니다.
117
117
alert(form.username==form.login); // true
118
118
*/!*
119
119
</script>
120
120
```
121
121
122
-
That's usually not a problem, because we rarely change names of form elements.
122
+
그런데 폼 요소의 이름을 변경하는 일은 드물기 때문에 보통은 이런 특징이 문제가 되지 않습니다.
123
123
124
124
````
125
125
126
-
## Backreference: element.form
126
+
## element.form으로 역참조 하기
127
127
128
-
For any element, the form is available as `element.form`. So a form references all elements, and elements reference the form.
128
+
모든 요소는 `element.form`으로 폼에 접근할 수 있습니다. 폼이 내부에 있는 요소 모두를 참조할 수 있듯이 각 요소 또한 역으로 폼을 참조할 수 있죠.
129
129
130
-
Here's the picture:
130
+
그림으로 나타내면 다음과 같습니다.
131
131
132
132

133
133
134
-
For instance:
134
+
예시:
135
135
136
136
```html run height=40
137
137
<form id="form">
@@ -140,55 +140,55 @@ For instance:
140
140
141
141
<script>
142
142
*!*
143
-
// form -> element
143
+
// 폼 -> 요소
144
144
let login = form.login;
145
145
146
-
// element -> form
146
+
// 요소 -> 폼
147
147
alert(login.form); // HTMLFormElement
148
148
*/!*
149
149
</script>
150
150
```
151
151
152
-
## Form elements
152
+
## 폼 요소
153
153
154
-
Let's talk about form controls.
154
+
이제 폼 조작에 사용되는 요소들에 대해 살펴봅시다.
155
155
156
-
### input and textarea
156
+
### input과 textarea
157
157
158
-
We can access their value as `input.value` (string) or `input.checked`(boolean) for checkboxes.
158
+
input과 textarea 요소의 값은 `input.value` (string) 또는 `input.checked`(boolean)을 사용해 얻을 수 있습니다.
159
159
160
-
Like this:
160
+
이렇게 말이죠.
161
161
162
162
```js
163
163
input.value = "New value";
164
164
textarea.value = "New text";
165
165
166
-
input.checked = true; // for a checkbox or radio button
166
+
input.checked = true; // 체크박스나 라디오 버튼에서 쓸 수 있습니다.
167
167
```
168
168
169
-
```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
+
```warn header="`textarea.innerHTML` 말고 `textarea.value`를 사용하세요."
170
+
`<textarea>...</textarea>`안의 값이 HTML이더라도 값을 얻을 때 `textarea.innerHTML`을 사용하지 말아야 합니다.
171
171
172
-
It stores only the HTML that was initially on the page, not the current value.
172
+
`textarea.innerHTML`엔 페이지를 처음 열 당시의 HTML만 저장되어 최신 값을 구할 수 없기 때문입니다.
173
173
```
174
174
175
-
### select and option
175
+
### select와 option
176
176
177
-
A `<select>` element has 3 important properties:
177
+
`<select>` 요소에는 세 가지 중요 프로퍼티가 있습니다.
178
178
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>`.
179
+
1. `select.options` -- `<option>` 하위 요소를 담고 있는 컬렉션
180
+
2. `select.value` -- 현재 선택된 `<option>` 값
181
+
3. `select.selectedIndex` -- 현재 선택된 `<option>`의 번호(인덱스)
182
182
183
-
They provide three different ways of setting a value for a `<select>`:
183
+
이 세 프로퍼티를 응용하면 아래와 같은 세 가지 방법으로 `<select>`의 값을 설정할 수 있습니다.
184
184
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.
185
+
1. 조건에 맞는 `<option>` 하위 요소를 찾아 `option.selected`속성을 `true`로 설정합니다.
186
+
2. `select.value`를 원하는 값으로 설정합니다.
187
+
3. `select.selectedIndex`를 원하는 option 번호로 설정합니다.
188
188
189
-
The first way is the most obvious, but `(2)` and `(3)` are usually more convenient.
189
+
세 방법 중 첫 번째 방법이 가장 확실하지만 두 번째나 세 번째 방법이 대체로 더 편리합니다.
190
190
191
-
Here is an example:
191
+
예시:
192
192
193
193
```html run
194
194
<select id="select">
@@ -198,16 +198,16 @@ Here is an example:
198
198
</select>
199
199
200
200
<script>
201
-
// all three lines do the same thing
201
+
// 세 가지 코드의 실행 결과는 모두 같습니다.
202
202
select.options[2].selected = true;
203
203
select.selectedIndex = 2;
204
204
select.value = 'banana';
205
205
</script>
206
206
```
207
207
208
-
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.
208
+
대부분의 다른 폼 조작 요소와 달리 `<select>`는 `multiple` 속성이 있는 경우 option을 다중 선택할 수 있습니다. `multiple` 속성을 쓰는 경우는 아주 드물지만, 쓰게 되다면 첫 번째 방법을 사용해 `<option>` 하위 요소에 있는 `selected` 프로퍼티를 추가·제거해야 합니다.
209
209
210
-
We can get their collection as `select.options`, for instance:
210
+
선택된 여러 개의 option이 담긴 컬렉션은 다음 예시처럼 `select.options`를 사용해 얻을 수 있습니다.
211
211
212
212
```html run
213
213
<select id="select" *!*multiple*/!*>
@@ -217,7 +217,7 @@ We can get their collection as `select.options`, for instance:
217
217
</select>
218
218
219
219
<script>
220
-
// get all selected values from multi-select
220
+
// 선택한 값 전체
221
221
let selected = Array.from(select.options)
222
222
.filter(option => option.selected)
223
223
.map(option => option.value);
@@ -226,72 +226,72 @@ We can get their collection as `select.options`, for instance:
226
226
</script>
227
227
```
228
228
229
-
The full specification of the `<select>` element is available in the specification <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
229
+
`<select>` 요소에 대한 명세는 아래 링크에서 볼 수 있습니다 <https://html.spec.whatwg.org/multipage/forms.html#the-select-element>.
230
230
231
-
### new Option
231
+
### Option 생성자
232
232
233
-
This is rarely used on its own. But there's still an interesting thing.
233
+
Option 생성자는 잘 사용되지는 않지만 흥미로운 점이 있습니다.
234
234
235
-
In the [specification](https://html.spec.whatwg.org/multipage/forms.html#the-option-element) there's a nice short syntax to create `<option>` elements:
235
+
[명세서](https://html.spec.whatwg.org/multipage/forms.html#the-option-element)를 보면 `<option>` 요소를 생성하는 간단하고 멋진 문법을 찾을 수 있죠.
236
236
237
237
```js
238
238
option = new Option(text, value, defaultSelected, selected);
239
239
```
240
240
241
-
Parameters:
241
+
매개변수:
242
242
243
-
- `text` -- the text inside the option,
244
-
- `value` -- the option value,
245
-
- `defaultSelected` -- if `true`, then `selected` HTML-attribute is created,
246
-
- `selected` -- if `true`, then the option is selected.
243
+
- `text` -- option 내부의 텍스트
244
+
- `value` -- option의 값
245
+
- `defaultSelected` -- `true`이면 HTML 속성 `selected`가 생성됨
246
+
- `selected` -- `true`이면 해당 option이 선택됨
247
247
248
-
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`).
248
+
`defaultSelected`와 `selected`의 차이가 무엇인지 헷갈릴 수 있습니다. `defaultSelected`는 `option.getAttribute('selected')`를 사용해 얻을 수 있는 HTML 속성을 설정해 줍니다. 반면 `selected` 는 option의 선택 여부를 결정합니다. 그렇기 때문에 당연히 `selected`가 더 중요한 매개변수이죠. Option 생성자를 사용할 때는 대개 두 매개변수 모두를 `true`나 `false`로 설정합니다.
249
249
250
-
For instance:
250
+
예시:
251
251
252
252
```js
253
253
let option = new Option("Text", "value");
254
-
// creates <option value="value">Text</option>
254
+
// <option value="value">Text</option> 가 생성됩니다.
255
255
```
256
256
257
-
The same element selected:
257
+
이번엔 같은 요소를 선택된 상태로 생성합니다.
258
258
259
259
```js
260
260
let option = new Option("Text", "value", true, true);
261
261
```
262
262
263
-
Option elements have properties:
263
+
Option을 사용해 만든 요소에는 다음과 같은 프로퍼티가 있습니다.
264
264
265
265
`option.selected`
266
-
: Is the option selected.
266
+
: option의 선택 여부
267
267
268
268
`option.index`
269
-
: The number of the option among the others in its `<select>`.
269
+
: option 중 몇 번째인지를 나타내는 번호
270
270
271
271
`option.text`
272
-
: Text content of the option (seen by the visitor).
0 commit comments