Tuesday 17 December 2019

javascript - JQuery .on method not actually detecting new elements

jQuery( document ).on( "click", "#starSort ", function( ) {

jQuery( ".qa-voting-bookmark.active" ).closest( ".answer" )
.prependTo( "#starbox" );

jQuery("#starbox").append(jQuery(".qa-voting-bookmark.active")
.closest( ".answer" ).get().reverse());
});



The above code is what I have.
it works fine before the new elements are created.
the current .answer elements are recreated by another function that refreshes the data.



i tried just going with.



    jQuery( "#starSort" ).on( "click", function( ) {});



but it doesn't like it.
Though the button isn't a concern, it doesn't become dynamic.
Only these .answer elements.



edit**
My event listener targets the whole body.
But when I read about Event Delegation, all the examples are about the button and the dynamic element being the same object.
I want to click a button that is not inside of the list of dynamic objects, and have it change the dynamic objects.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...