0

I want to create a new div tag via

$(this).prepend("<div class='test_close'></div>");

and onclick it should close

$(function() {
    $(".test_close").click(function() {
        console.log("clicked");
        $(this).remove();
    });
});

but it doesn't work for me...

do you have an idea how i can do this?

thanks in advance and sorry for my bad english

Mustang96
  • 39
  • 1
  • 4
  • This question was asked many-many times. Several times every day. For dynamically created elements use delegated event handlers. For example, using `$(document).on("click", ".test_close", function() {` – Regent Oct 20 '14 at 09:46

1 Answers1

0

Use .on() to attach events to dynamically created elements. Try this:

$(document).on("click",".test_close",function() {
        console.log("clicked");
        $(this).remove();
});

PS: To increase performance replace document with .test_close div parent element in above code snippet.

Kiran
  • 20,167
  • 11
  • 67
  • 99