How can I use a modal window to display the current post in loops featured image?

I am trying to create a page in my WordPress theme where the posts are displayed in CSS grid. When a grid square is clicked the featured image for the clicked post will appear in a modal window. I have managed to get the modal window working but it displays the featured image of the most recent post regardless which post is selected. I tried assigning a unique id to image div using get_post_id but I can’t work out how to pass this to the JavaScript’s getElementById? (if that is in fact the correct way to solve this?)

I’m a newbie coder and not so familiar with JavaScript – any help would be hugely appreciated.

Here’s my code: