728x90
getElementById 함수를 사용한 HTML 엘리먼트 선택방법
HTML 코드
1
2
3
4
5
6
7
8
9
10
11
|
<section id ="section1">
<h1>Ex1 : 엘리먼트 선택방법</h1>
<div>
<input id="txt-x" type="text" value="0" dir ="rtl">
+
<input id="txt-y" type="text" value="0" dir ="rtl">
<input id="btn-add" type="button" value="=">
<input id="txt-sum" type="text" value="0" dir ="rtl" readonly>
</div>
</section>
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
자바스크립트 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// Ex1: 계산기 프로그램
window.addEventListener("load", function() {
var txtX = document.getElementById("txt-x");
var txtY = document.getElementById("txt-y");
var btnAdd = document.getElementById("btn-add");
var txtSum = document.getElementById("txt-sum");
btnAdd.onclick = function () {
};
});
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
"load" 함수를 이용해 html이 모두 load 되었을 때 실행되게 한 후에 html 태그의 id값에 맞춰 객체를 읽어온다.
받아온 객체 btnAdd 클릭 하였을 때 txtX에 적힌 값과 txtY에 적힌 값을 숫자로 변환한 후에 더한 값을 txtSum.value를 통해 html input 창에 입력해준다.
결과화면
반응형
'2019백업' 카테고리의 다른 글
Javascript 엘리먼트 선택방법 (3. Selector API Level1) (0) | 2019.07.23 |
---|---|
Javascript 엘리먼트 선택방법 (2. getElementById 개선하기) (0) | 2019.07.23 |
자바스크립트 - Object, JSON (0) | 2019.07.23 |
자바스크립트- `${}`Template Literals (0) | 2019.07.23 |
자바스크립트 - Array (0) | 2019.07.23 |