화면이 로딩된 후 추가된 div태그나 버튼에 이벤트를 등록할 경우가 있다. jQuery에서는 $(…).on(‘event’, ‘target’, function() {…}); 함수를 사용한다.
<html> <head> <title>동적 추가된 div태그에 이벤트 추가</title> <style type="text/css"> .dynamicDiv { width:170px; background-color: #CBE7F4; text-align:center; font-weight:bold; padding: 3px 3px; cursor:pointer; margin:5px; border-radius:.25em; border:1px solid #449999; } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script language="javascript"> $(document).on('click', '.dynamicDiv', function() { console.log("동적 추가된 div태그 '"+$(this).attr("id")+"'이 클릭되었습니다."); }); var index = 0; var addDiv = function() { $("body").append("<div class='dynamicDiv' id='"+index+"'>동적추가 div " +index+ "</div>"); index++; }; </script> </head> <body> <input type="button" onclick="addDiv()" value="Div 태그 추가"> </body> </html>
‘Div 태그 추가’버튼을 클릭하면 동적으로 ‘동적추가 div’ div가 화면에 추가된다. 추가된 div를 클릭하면 $(document).on(…);에 의해서 추가된 click이벤트가 실행된다.
예제에서는 click이벤트가 dynamicDiv라는 클래스를 가진 Div태그에 등록되었다.

- jQuery checkbox 전체 선택/해제 예제 2022년 3월 15일
- Highcharts error #17 2019년 7월 10일
- Highchart TypeError: c.init is not a function 2019년 7월 10일
- Highchart 크기 조정 2019년 7월 8일
- Highcharts error #13 2019년 7월 8일
- Grid Stack Two grids demo 예제 2019년 6월 12일
- 동적으로 추가/생성된 태그에 이벤트 추가 방법 2019년 6월 10일
- Highcharts update event 2019년 3월 26일
- jdom2를 이용한 spring xml response(응답) 2017년 12월 5일
- jquery xml 요청, spring xml 리턴 2017년 12월 5일
- jquery div show hide 예제 2016년 5월 25일
- jquery css 예제 2016년 5월 25일
- jquery selectbox 예제 2016년 5월 25일
- jquery json 예제 2016년 5월 25일
- jquery template 예제 1 2016년 5월 25일
- jquery datepicker 2016년 5월 9일
- jquery radiobox 2016년 4월 29일
- jquery bgiframe 정리 2016년 2월 1일
- jquery each( $.each ) 2016년 1월 25일
- jquery spring – checkbox로 선택된 배열 주고 받기 2015년 8월 15일
- jquery spring ajax call 2015년 8월 15일
- jQuery 동적 체크박스 제어 2015년 1월 27일
- 간단한 jquery chained selectbox 예제 2014년 12월 4일
- jquery ui tip 2014년 9월 2일