veggie의 기술기록
[JavaScript] document.forms 객체 본문
반응형
자바스크립트에서는 form에서 사용되는 양식들을 forms 객체에서 정의할 수 있다.
forms 객체는 links, images 등과 같은 배열 형태로 document 객체의 하단에 존재하는 객체이다.
⊙ forms[]
form의 구성요소처럼 폼 자체도 배열로 참조되어질 수 있고, document.forms[0] 과 같은 방식으로 접근할 수 있다.
물론 배열로 참조된 폼 내에서도 elements[] 배열을 사용할 수 있다. document.forms[0].elements[1] 방식으로 접근한다.
form에서 사용되는 양식들의 name을 정의해준다면, 자바나 자바스크립트에서 더욱 쉽게 접근 가능하다.
하지만 name을 정의하지 않았더라도 참조 가능하다.
ex)
<form> =>document.forms[0]
<input type="text" value=${text1}/> =>document.forms[0].elements[0]
</form>
<form> =>document.forms[1]
<input type="text" value=${text1}/> =>document.forms[1].elements[0]
</form>
하지만 코드의 가독성 및 생산성 향상을 위해 name을 정의해 주는 습관을 들이도록 하자!
출처 : https://m.blog.naver.com/PostView.nhn?isHttpsRedirect=true&blogId=akira54055&logNo=60035061694
반응형
'IT_JavaScript' 카테고리의 다른 글
[JavaScript] 평가전략 - 'Call By Value' vs 'Call By Reference' (0) | 2021.10.04 |
---|---|
[JavaScript] 'Parameter' vs 'Arguments' (0) | 2021.10.04 |
[JavaScript] 호이스팅(Hoisting)이란? (0) | 2021.10.03 |
[JavaScript] 배열과 객체 (0) | 2021.09.26 |
[JavaScript] form태그의 target 속성 (0) | 2021.09.25 |
Comments