Suppose that I use AJAX to dynamically inject a gallery of images into a page. Note that I don't just inject the images, but the whole gallery's markup, like this:
<div id="gallery">
<img src="image-1.jpg" />
<img src="image-2.jpg" />
<img src="image-3.jpg" />
</div>
The following code wouldn't work, because on document ready the gallery is not there yet, so the gallery
variable that I call on click is actually empty, right?
$( document ).ready( function() {
var gallery = $( '#gallery' );
$( '#button' ).on( 'click', function() {
gallery.css( 'display', 'block' );
});
});
My question is: is there a way to use a variable as if it was a placeholder for an element that is dynamically generated at a later moment, like in the above code?