javascript - Adding Click Function to Dynamically Generated div jquery -


i new jquery , having issues binding functions dynamically generated div.

essentially user clicks button adds couple divs container , changes background color of container. want them have option remove added clicking on 1 of divs.

i have function thought should that:

$('.course-delete').click(function() {     var course = $(this).parent();     alert("hello");     $(course).css('background', "#fff");     $(course).empty();   }); 

but whatever reason nothing happens when click div. if knows going on appreciate it.

by sounds of it, .course-delete elements don't exist when jquery attempts bind handler - because divs created dynamically later. if issue, event delegation saviour: https://learn.jquery.com/events/event-delegation/

$(document).on('click', '.course-delete', function () {     /* delete stuff here */ }); 

Comments

Popular posts from this blog

css - I want to align grid in center -

Contact Form PHP Email Script -

python - SWIG function not printing output -