js

[jQuery] 동적 추가된 append에 click event 처리

hellooooo 2021. 10. 26. 17:15
728x90

append 한 button에 클릭이벤트를 주니 이런 오류 메시가 출력. 

(index):1 Uncaught ReferenceError: 함수명 is not defined
    at HTMLButtonElement.onclick ((index):1)

원인 : 동적으로 추가된 태그는 이벤트가 동작되지 않음. = 로드되었을 때 존재하지 않는 태그에 대해서는 이벤트를 걸 수 없는 것

 $(document).on('click / change  등등의 이벤트', '이벤트 적용할 타깃 태그', 동작 함수(){});

또는 

$('.부모 이름').on('click', '.걸고 싶은 대상', function() {
 넣을 소스;
});

 

 

 

출처 : https://stackoverflow.com/questions/15420558/jquery-click-event-not-working-after-append-method

 

Jquery click event not working after append method

So I have this button which adds a new row to the table, however my problem is that it does not listen to the .new_participant_form click event anymore after the append method has taken place. htt...

stackoverflow.com