Help! I'm receiving a modal error message requesting an ID for the correct modal view. How can

Here's the code snippet I've been working on:

<!DOCTYPE HTML>

<html>
<head>
<title> EURO CATALOGO </title>

<style>
#myImg {
    border-radius: 5px;
    cursor: pointer;
    transition: 0.3s;
}

#myImg:hover {opacity: 0.7;}

/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}

/* Modal Content (image) */
.modal-content {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
}

/* Caption of Modal Image */
#caption {
    margin: auto;
    display: block;
    width: 80%;
    max-width: 700px;
    text-align: center;
    color: #ccc;
    padding: 10px 0;
    height: 150px;
}

/* Add Animation */
.modal-content, #caption {    
    -webkit-animation-name: zoom;
    -webkit-animation-duration: 0.6s;
    animation-name: zoom;
    animation-duration: 0.6s;
}

@-webkit-keyframes zoom {
    from {transform:scale(0)} 
    to {transform:scale(1)}
}

@keyframes zoom {
    from {transform:scale(0)} 
    to {transform:scale(1)}
}

/* The Close Button */
.close {
    position: absolute;
    top: 15px;
    right: 35px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
}

.close:hover,
.close:focus {
    color: #bbb;
    text-decoration: none;
    cursor: pointer;
}

/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px){
    .modal-content {
        width: 100%;
    }
}
h1
{
text-align:center;
}

h2
{
text-align:center;
font-size:20px;
}
table, th, td 
{
border-collapse:collapse;
    border: 1px solid black;
}
th
{
width: 50px;
height: 50px;
}
</style>
</head>
<body>

<h1><img src="http://www.megghy.com/gif_animate/cose_oggetti/euro/24.gif"></img> EURO CATALOGO <img src="http://www.megghy.com/gif_animate/cose_oggetti/euro/24.gif"></img> <h1>
<h2> di STEFANO LIMONE <h2>


<!-- The Modal -->
<div id="myModal" class="modal">
  <span class="close">×</span>
  <img class="modal-content" id="img01">
  <div id="caption"></div>
</div>
<table>
  <tr>
    <th></th>
    <th>2004</th><th>2005</th><th>2006</th><th>2006</th><th>2007</th><th>2008</th><th>2009</th><th>2009 UEM</th><th>2010</th><th>2011</th><th>2012</th><th>2012 TYE</th><th>2012</th><th>2013</th><th>2014</th><th>2014</th><th>2015</th><th>2015</th><th>2015 flag</th> <th> 2016 </th>
  <tr>
    <td>ANDORRA</td>
    <td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td><img id="myImg" src="ANDORRA/2euro2014.jpg" alt="20° anniv. ingresso Andorra nel Consiglio europeo"
 width="300" height="200"></td>
  </tr>
  <tr>
    <td>February</td>
    <td>$80</td>
  </tr>
</table>
<script>
// Get the modal

var modal = document.getElementById('myModal');
var id;

// Get the image and insert it inside the modal - use its "alt" text as a caption
function handleClick(imgID) {
    id = imgID;
    var img = document.getElementById(id);
    var modalImg = document.getElementById("img01");
    var captionText = document.getElementById("caption");
    img.onclick = function(){
        modal.style.display = "block";
        modalImg.src = this.src;
        modalImg.alt = this.alt;
        captionText.innerHTML = this.alt;
    };
}

handleClick('anotherImg'); // Call the function with a different ID here

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks on <span> (x), close the modal
span.onclick = function() { 
  modal.style.display = "none";
}


</script>

I'm interested in changing the ID used at line 148 :

var img = document.getElementById("myImg");

How can I pass a different ID? I am attempting to create an onClick function that passes "this.id" in an img and saves this.id in a variable=id... but it is not being read in the instruction at line 148. Why is that?

Any assistance you can provide would be greatly appreciated.

The modal functionality works for a single image with the same ID.

Answer №1

Below is the provided code snippet:

function displayImage(imageId) {
var modal = document.getElementById('myModal');

// Retrieve the image and place it inside the modal - utilizing its "alt" text as a caption
var image = document.getElementById(imageId);
var modalImage = document.getElementById("img01");
var captionText = document.getElementById("caption");
image.onclick = function(){
    modal.style.display = "block";
    modalImage.src = this.src;
    modalImage.alt = this.alt;
    captionText.innerHTML = this.alt;
}

// Obtain the <span> element responsible for closing the modal
var closeBtn = document.getElementsByClassName("close")[0];

// Upon clicking on the <span> (x), the modal will be closed
closeBtn.onclick = function() { 
  modal.style.display = "none";
}
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error - The function of spread syntax necessitates an iterable item with a Symbol iterator to be a function

I encountered an error while trying to use the update component in my app, and I am unsure of why this error is occurring. Error: TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function at HttpHeaders.applyUpdate (http.mjs:244: ...

Is there a way to efficiently pull specific time-related data from XML API forecasts using PHP and present them in HTML tables?

Currently, I'm facing a bit of confusion on how to obtain a forecast for the upcoming 5 days of a specific city using OpenWeather's forecast api. The challenge lies in the fact that the forecasts are provided in 3-hour intervals, but I only requi ...

AngularJS: Sending form data to an external server results in an error due to restricted URI access

I am currently diving into the world of AngularJs and putting effort into mastering it. In this process, I've created a demo application that attempts to send form data to a localhost site. The code for this endeavor is provided below. index.html &l ...

Guide to incorporating a rectangular image into a circular frame with CSS

I have a rectangular image with dimensions of 300px by 300px. To display this image within a circle, I have created the following CSS code: img { width: 300px; height: 300px; background-color: white; border-radius: 50%; border: 2px solid black; ...

In Angular 4, when choosing an option, only the Display Id is shown, not the Name

I'm currently working with Angular 4 Web API to display packingtypename when an option is selected. Everything is working fine, but I am facing an issue where only the packingtypeID is saved when clicking the add button. I would like to save both the ...

React, encountering issues when displaying images with spaces in their filenames

I encountered an issue with my React-Express application - images with spaces in their names are not displaying properly within the application, despite having the correct path. On the other hand, images without spaces in their names are rendering correctl ...

It's recommended to utilize the callback feature with setLoggedIn to ensure the previous state is captured effectively, as illustrated below:

To ensure capturing the previous state when using setLoggedIn, it is recommended to utilize the callback option. This will help in keeping track of changes and ensuring smoother functionality: ...

Sharing a Detailed Mongoose Schema

Hey there! So, I've been working on this Mongoose Schema and there seems to be something off about it. The main area of concern is the "region" part. const mongoose = require('mongoose'); const destination = new mongoose.Schema({ ...

Zone.js error: Promise rejection caught

When I call a function from an external library on page load in my Angular application, Chrome dev tools console shows the error message: "Unhandled Promise rejection: Cannot read properties of undefined (reading 'page') ' Zone: <root> ...

The PrimeNG table component in Angular is having trouble maintaining a fixed width

I am facing an issue with fixing the width of the first column in a primeNG table. Despite inspecting the element and confirming that it is displaying correctly, my CSS adjustments are being overridden. Essentially, I am seeking a CSS solution where, rega ...

Customize items within a bootstrap5 navbar by utilizing traditional CSS styling techniques

Hello everyone, I'm currently working on creating a navigation bar using Bootstrap 5. I am looking to customize the colors of certain elements such as the navbar, tags, navbar background, and burger menu. I've attempted to adjust the tags' c ...

Animated div or fieldset featuring a multi-step form

I have recently put together a detailed step-by-step guide. Each step is wrapped in its own "fieldset" within a single .html file. To navigate back and forth, I have incorporated javascript into the process. However, as time goes on, I am noticing that th ...

Tips for incorporating padding when an h1 title spans multiple lines

I'm facing an issue with a div that has a width of 46%. When the text in the h1 tag is too long, it gets broken into multiple lines and the left padding is not consistent. Is there a way to maintain the same left padding for the second line? To help ...

Creating an Editor for Input Text Field in HTML: A Step-by-Step Guide

In the vast landscape of JS libraries that can achieve this function, like Trumbowyg and more. However, prior to my rails project displaying that slim version, I need to ensure JavaScript is properly escaped! Therefore, I need to create an editor using o ...

Unforeseen rotation happening in CSS animation

I find myself struggling with my math skills from junior high. My goal is to rotate two divs around the Y axis in opposite directions. I have set up two animations for this purpose: @-webkit-keyframes first{ 0% { -webkit-transform: rotateY(0); } ...

When copying text from React-PDF Display, the values may appear altered or varied

This snippet of text was excerpted from a brief SHA provided at the git summit. Generated using React-pdf, this is a PDF document with some interesting quirks. Although the displayed text reads as 4903677, it changes to •G07THH when copied. The font ...

Store the fetched data as JSON in the local storage

Struggling to find a solution after reading several articles, I am working on fetching a large JSON from an API and I want to cache the response in the localStorage. The goal is for the script to check for an object with the requested ID in the cached JSON ...

What is the best way to halt a function's execution upon the occurrence of an event?

Hello everyone, I have a task where I need to stop a function process when a specific event occurs. Typically, if an error is thrown within the process (for example, from the delay function in one of the steps), the process will terminate without any issu ...

Could someone lend a hand in getting the X to align properly on this div? I've been struggling with it for ages and just can't seem to crack

This is a demonstration of the CodePen project: Click here to view <div class="annotation"> <div class="annotext"> <div class=annobar></div> <div class="x">✕</div> Lorem ipsum dolor sit amet, consectetur adipiscing e ...

Downloading a Facebook profile picture is a time-consuming task

I have a simple Christmas themed photo editing website that allows users to create holiday images. To achieve this, I require users to save their Facebook profile picture. However, the process is taking much longer than expected, usually between 15-30 seco ...