본문 바로가기
Language&FrameWorks/JavaScrip

DOM 을 사용한 Input 객체 추가

by 감마 2010. 3. 25.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">
<head>
<meta http-equiv="content-type" content="text/html; charset=euc-kr" />
<title>DOM을 사용한 변경</title>
<script type="text/javascript">
var count = 0;
function appendItem() {
count++;
var newItem = document.createElement("div");
newItem.setAttribute("id", "item_" + count);
var html = '새로 추가된 아이템['+count+']'+
'<input type="button" value="삭제" onclick="removeItem(' + count + ')"/>';
newItem.innerHTML = html;
var itemListNode = document.getElementById('itemList');
itemListNode.appendChild(newItem);
}
function removeItem(idCount) {
var item = document.getElementById("item_"+idCount);
if (item != null) {
item.parentNode.removeChild(item);
}
}
</script>
</head>
<body>
<input type='button' value='추가' onclick='appendItem()' />
<div id="itemList"></div>
</body>
</html>

'Language&FrameWorks > JavaScrip' 카테고리의 다른 글

[JQuery] SelectBox 선택된 값  (0) 2010.05.26
JavaScript 카운트다운 스크립트  (0) 2010.04.29
한글입력  (0) 2009.10.12
마우스 제어 스크립트  (0) 2009.10.12
특수문자처리  (0) 2009.10.12

댓글