0

I am creating new <span> inside a <div id="boxclass"></div> with jQuery like this:

$("#boxclass").append("<span id='test'></span>");

But I cant access the newly created spans on click.

$( "#test" ).click(function() {

    alert("Clicked on appended span");

});

How can i solve this?

Thank you.

  • Do you execute your listener AFTER the append? the $("#test") is executed BEFORE the append, they are not related and it doesnt work – cucuru Jan 03 '18 at 15:56
  • I added a listener after the main
    and it worked: `document.getElementById("boxclass").addEventListener("click", function)` Thank you.
    – Andris Kovalevskis Jan 03 '18 at 16:28
  • I usually create a addListenerFunction(){$("#test").click().....} and called after the append. If you do in this way remember to remove the listener if added, or the code will execute twice – cucuru Jan 03 '18 at 16:48

0 Answers0