[JS] 자바스크립트 HTML 요소 선택 getElementById 실습하기 - 1

 

 

getElementById  Id 값을 이용해서 실습해보기

 

 

Id 값을 읽어 들여서 id 값으로 반환하기

3+2 = 결과값 반환 5

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>

<script>

function check(){
	
	var x = document.getElementById("a").value;
	var y = document.getElementById("b").value;
	
	var sum = parseInt(x)+parseInt(y);
	document.getElementById("sum").value = sum;
}

</script>


</head>
<body>


<form name="test">

<input type="text" id="a"><br>
<input type="text" id="b"><br>
<input id="sum"> <!-- a+b 의 값이 나타난다 --><br>
<input type="button" value="사칙연산" onclick="check()">

</form>


</body>
</html>