Tips on displaying an image from an array in JavaScript

I am working on a page that showcases items from an array list, each with an image capped at 120px. I am looking for a way to have the image pop up in the center of the screen with a larger size without relying on plugins. Although there are plugins available for this purpose, I prefer to achieve it through coding.

Here is the structure of my code:

<div id="animallist"></div>

Incorporated JavaScript:

const animals = [{
    name: "Cat",
    useful: "no",
    image: "https://icatcare.org/app/uploads/2018/07/Thinking-of-getting-a-cat.png"
  },
  {
    name: "Dog",
    useful: "yes",
    image: "https://post.medicalnewstoday.com/wp-content/uploads/sites/3/2020/02/322868_1100-1100x628.jpg"
  },
  {
    name: "Fish",
    useful: "no",
    image: "https://cdn0.wideopenpets.com/wp-content/uploads/2019/10/Fish-Names-770x405.png"
  }
]

animals.forEach(addLink);

function addLink(animal, i) {
  const div = document.createElement('div');
  const animalList = document.createElement('h2');
  const image = document.createElement('img');
  image.id = "image";
  animalList.innerHTML =  animal.name + " " +"-"+"useful?" + " "+ animal.useful;
  animalList.style.cssText = "text-align:center;"
  image.src = animal.image;
  div.appendChild(image);
  div.appendChild(animalList);
  div.dataset.animalName = animal.name;
  animallist.appendChild(div);
}

View live demo on jsfiddle

Answer №1

To implement a 'pop-up' div, start by setting its class to 'display: none'. Then, using JavaScript, remove the 'display: none' class and update the inner img src with your desired image. Below is the functional code snippet for achieving this: https://jsfiddle.net/bcdu3L2j/1/

const images = document.getElementsByTagName('img');
const imagesArray = Array.from(images);

const popUpDiv = document.getElementById('pop-up');
const popUpImg = document.querySelector('#pop-up img');

function displayPopUpImage(event) {
  const imageURL = event.target.src;
  popUpImg.src = imageURL;
  popUpDiv.classList.remove("hidden");
}

imagesArray.forEach((image) => {
  image.addEventListener('click', displayPopUpImage);
});

popUpDiv.addEventListener('click', () => {
  popUpDiv.classList.add("hidden");
});

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

Having issues with $emitting not working for parent-child components in Vue. Any ideas on what I might be doing incorrectly?

I have a login component that I need to call in the main vue component of App.vue. Within the login vue, when I click on any button, it should activate another vue component using Vue.js router to replace the login page. I have searched for solutions but h ...

How can I align the title of a cell to the left while centering the remaining content?

I have a dilemma with two table cells placed side by side that stack upon triggering a media query for an HTML newsletter. I wish to align the headlines "Be Ready" and "Stay Organized" to the left when the responsive code activates, but the use of "margin: ...

Curved lines on canvas

I am currently using the stroke and path features in the canvas to create two lines, with the intention of giving them a curved, wave-like effect. I would like to achieve this without having to create an actual image in Photoshop. Is there anyone who can ...

Setting up a personalized configuration entry in environment.js

I am currently working with EmberJS version 2.4.2 and I have a specific requirement to handle custom configuration entries using an environment.js file. var ENV = { APP: { myKey: "defaultValue" } }; While everything works perfectly in development ...

What is the process for removing specific words from the text?

I am looking for a solution to select words and create an array out of them. Once the words are added to the array, I want to delete them, but I'm unsure of how to do that using either document.getSelection() or document.selection. If anyone has any h ...

Changing the selection of a ComboBox in WebDriver using the "webkit-user-select" attribute

I attempted to alter the selection of a combobox with a "webkit-user-select" type in various ways, but unfortunately, none of my attempts were successful. 1) import org.openqa.selenium.support.ui.Select; Select select = new Select(driver.findElement(By. ...

Scripted AngularJS directive

Essentially, the directive I have defined as sample.js is: app.directive('myDirective', function() { return { restrict: 'E', scope: { info: '=' }, templateUrl: 'link-to-sam ...

$(document).on("click" .. selecting first this and then that specific element

I have a single checkbox and two radio buttons to choose from. If the user selects "wedding" from the checkbox and "boys" from the first radio button, I want to display another set of radio options under the class ".coaches". Although I've followed ...

Does every controller page need to verify the Login Function?

In my PHP pages, I have implemented the MVC Pattern by creating a controller page for each view page to interact with the Model page. However, I have only checked user login at the top of every view page and not in the controller page. This leaves a potent ...

JQuery Ajax: The loaded content flickers into view, revealing old content momentarily

Having an issue with my ajax code. I am working on a project that involves some links and a content area. The idea is that when a user clicks on a link, the content area should be hidden, load new data, and then show the updated content. However, I have no ...

A guide on sorting through categories in Angular 9

Having trouble filtering categories in a Webshop? I've been following a tutorial by Mosh but I can't seem to get it right. No error messages but nothing is being filtered or displayed. I'm brand new to Angular and/or typescript, so please be ...

Navigating JSON data to create interactive ejs code snippets with dynamic content

Okay, I seem to have gotten in over my head with this one (or maybe it's just late and my brain is fried). So, I have a JSON response that has this structure: -OBJ -Random Data -Results (array) -0 -1 -2 -and so on -data ...

React checkbox showing inaccurate data

I have created a React component called RidiculousCheckbox: export default class RidiculousCheckbox extends Component { constructor(props) { super(props); this.state = { isChecked: true, }; } handleInputCha ...

Is it possible to utilize $(document).ready() within an ASP.NET page?

I am encountering an issue while attempting to use $(document).ready() as shown in the image above. What steps should I take to troubleshoot and resolve this problem? Update 23/05/2011 10:54 A new insight has come to light. The page I am working on inher ...

Issue encountered when trying to serve firebaseui.css in VueJS using a node module

I've been facing this issue for a while now, and despite my attempts to resolve it, the problem persists. Originally, I followed this guide to develop my site as a PWA using vue-cli. For integrating firebase authentication, I referred to this guide, ...

Making adjustments to a row in the free jqGrid is a breeze with the ability

Using free jqGrid 4.12.1, I aim to incorporate functionality for adding, editing, and deleting rows in the grid with server-side calls for each operation. Below is the implementation of editurl and 'actions' formatter, { name: "actions", wi ...

Guide on incorporating foreign keys into conditional statements in Django HTML pages

Can anyone tell me why this condition isn't working? In the h4 tag, "Receita" is being displayed for lancamento.tipo, but the condition is not being met. Any help would be greatly appreciated. lancamento_list.html <div class="list-group&q ...

Struggling to generate a div using my JS/jQuery code

Currently working on a post-it web application to improve my skills in JavaScript and jQuery. I've encountered a few errors that have me stumped. Although I'm new to StackOverflow, I often use it as a helpful resource. You can find the code here ...

Transmit information across disparate components in Vue.js

This situation involves a Graph component displayed in the body of the page, allowing user modifications. Additionally, there is a Search component located in the header of the page. These two components are independent - Graph is exclusive to the body o ...

Refresh all the data points displayed on the c3.js chart

I created a basic div element in my HTML code: <div class='all'> Show all </div> Accompanying that, here is the JavaScript code I implemented: $(document).ready(function () { $('.all').click(function(){ ch ...