What is the best way to iterate through the images by utilizing the class trigger?

How can I cycle through all the images using a call trigger? When I click on an image, a modal popup should display both text and the image.

var modal = document.querySelector(".modal");
var trigger = document.querySelector(".trigger");
var closeButton = document.querySelector(".close-button");

function toggleModal() {
  modal.classList.toggle("show-modal");
}

function windowOnClick(event) {
  if (event.target === modal) {
    toggleModal();
  }
}
trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", toggleModal);
window.addEventListener("click", windowOnClick);
}
<div portfolio-item "> 
    <a class="trigger "><img src="images/techify.jpg "/></a>
    <div class="modal ">
    <div class="modal-content "> 
    <img class="modalimages " src ="Images/techify.jpg ">
    <div class="modal-text ">
    <p> It is all about a training institute</p>
    </div> 
    </div>
    </div>
    </div> 
    <div class="modal-content "> 
    <img class="modalimages " src ="Images/HairStyling.jpg "> 
    </div>
    </div>

Answer №1

Based on the information provided, there are a few minor issues that need to be addressed before this code can run smoothly. I made some adjustments to fix these issues, such as correcting the placement of } at the end of the script and adding a missing double quote in <div portfolio-item">.
It's unclear if this modification aligns with your desired outcome, so more details would be helpful.

<style>
.modal { display: block; }
.show-modal { display: none; }
</style>

<div portfolio-item> 
  <a class="trigger">
    <img src="images/techify.jpg"/>
  </a>
  <div class="modal">
    <div class="modal-content"> 
      <img class="modalimages" src="Images/techify.jpg">
      <div class="modal-text">
        <p> It is all about a training institute</p>
      </div> 
    </div>
  </div>
  <div class="modal-content"> 
    <img class="modalimages" src="Images/HairStyling.jpg"> 
  </div>
</div>  
<button class="close-button">Close Button</button>

<script>
var modal = document.querySelectorAll(".modal");
var trigger = document.querySelector(".trigger");
var closeButton = document.querySelector(".close-button");

function toggleModal() {
 for(i=0;i<modal.length;i++){
    modal[i].classList.toggle("show-modal");
  }
}

function windowOnClick(event) {
  if (event.target === modal) {
    toggleModal();
  }
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", toggleModal);
window.addEventListener("click", windowOnClick); 
</script> 

If you want both images to be hidden upon clicking the first image or pressing the button (which I added), you should relocate the following code up by one line:

<div class="modal-content"> 
  <img class="modalimages" src="Images/HairStyling.jpg"> 
</div>

Answer №2

To attach an event to each image, you can utilize JavaScript in the following manner:

var images = document.querySelectorAll("img")

for( var i = 0 ; i < images.length ; i++ ) {
    images[i].addEventListener("click", someFunction)
}

By doing this, whenever an image is clicked, it triggers the someFunction function.

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

Discovering the final element of ng-content while conducting tests using Protractor

I am searching for the always last element to click on. This is what I see when I inspect the page. https://i.sstatic.net/DMXQ8.png https://i.sstatic.net/ZkbER.png ...

Guide on querying a single document in MongoDB that contains a specific value for an element within an array of arrays

I am facing an issue with a document that has an array of arrays and is using embedded documents in MongoDB. The collection name Orders looks like this: "_id" : "HjPGrdkffg7dQPtiX", "ListOrdersResult" : [ { "Orders" : { ...

Click once to change the element, but it will remain changed and will not revert back no matter how many times you

After the first click displaying "X" and then changing to "O" in my tic-tac-toe JavaScript game, subsequent clicks only show "O", failing to switch back to "X". I am currently working on creating a tic-tac-toe game using JavaScript. Upon the first click, ...

What steps should I take to ensure that this countdown is continuously updating every second?

I created a function that's functioning properly, but I'm looking to update the message every second. Previously, I attempted using setInterval(countdownTimer(), 1000), however, it was unsuccessful. Below is my code snippet! let x = await msg.c ...

What is the best way to expand a Modal in Bootstrap 4?

Initially, I am facing an issue with Object-Oriented Programming in javascript, and my understanding of it is quite limited. However, I urgently need to resolve this problem... Currently, I am attempting to utilize a library designed for Bootstrap 3 withi ...

Placing a picture within a box that is 100% of the viewport height

After searching for a while, I still haven't found a solution to my problem. I am currently working on a Personal portfolio that has different sections with a height of 100vh each. However, I am facing difficulty in positioning the images within the s ...

Is there a way to utilize a single function on two separate div elements?

Looking for a way to optimize my code that contains the addRow() and deleteRow() functions. Currently, I have duplicated these functions for both radio buttons and checkboxes. Is there a more efficient way to achieve this? function addRow(tableID) { ...

Node JS is causing an error with an undefined value on line 1, displaying [

Hey there, I'm in the process of developing a discord bot and encountering an error that I can't seem to figure out. As a beginner, I've been relying on online tutorials to guide me through this project. Can someone help me understand why th ...

What steps should I take to resolve the problem with accessing Mongodb?

Whenever I attempt to initialize MongoDb by typing 'mongo', I keep getting the error message "not recognized as an internal or external command". Despite setting the environment variables as recommended by many, as shown in this screenshot, I am ...

Setting document expiration with MONK: A step-by-step guide

I recently went through the MongoDB documentation to understand how to set a Time To Live property for expiring documents. The documentation explains: In order to expire data after a certain number of seconds have passed since the indexed field, you ne ...

Safari not properly adjusting height of bootstrap cards within column in row

I'm currently working on a bootstrap and angular project. I've noticed that in Safari, the card heights are not behaving as expected compared to other browsers like Firefox, Chrome, and Edge. I have tried setting the height to 100%, but it seems ...

The jQuery countdown plugin is yielding some unexpected outcomes

Feeling a bit rushed for time, so I thought I'd ask here. The date is currently 2012-10-06 and I'm attempting to implement a jQuery plugin called "jquery.countdown.js". It seems pretty straightforward. Can anyone point out what I might be doing i ...

Tips for arranging Angular Material cards in columns instead of rows

I am currently developing a web application using Angular, and I've encountered an issue while trying to display cards in a vertical layout rather than horizontally. My goal is to have the cards fill up the first column (5-6 cards) before moving on to ...

Choose all pseudo elements from elements belonging to the class or ID labeled as 'x'

I am seeking a solution to target all the pseudo elements of an element that carries the class galleryBlock, as well as a method to target all the pseudo elements of an element with a specific id. My code structure consists of a grid made up of multiple i ...

CSS and JavaScript dropdown malfunctioning due to a position swap in internal CSS

This example demonstrates functionality .c1 { background-color:red; position:relative; } .c2 { background-color:blue; position:absolute; height:50px; width:100px; display:none;} .show { display:block; } <body> <button ...

What could be the reason behind the for loop not incrementing and the splice not being modified in my code?

Encountered a seemingly simple problem while coding, but the expected returns started deviating as I progressed. Your assistance would be greatly appreciated. If you do provide help, please elaborate on your approach and where I hit a roadblock. The Chal ...

Showing the total quantity of products, reminiscent of a virtual shopping basket

I am trying to show a number similar to how a shopping cart displays items. The php code I was given currently shows a cookie value, which is mostly functional. However, if you encounter errors while adding items to the cart, it increases the cookie count ...

Encountering difficulties running the build post installation of the react-native-jsi-image package

Upon attempting to install the npm package react-native-jsi-image on Android and running npx react-native run-android, my build fails with an error. I am seeking assistance in resolving this issue. Currently, I am using the latest version of React Native C ...

What is the process of incorporating external links into an angular application?

Attempting to embed an external URL within my Angular app using an iframe has resulted in the following issue: https://i.sstatic.net/liSmX.png The error message reads as follows: https://i.sstatic.net/u9GWw.png Below is the template where I am trying t ...

Aligning variable width numbers within a div container

Currently experimenting with CSS to create a calendar design that mimics the look of a traditional paper calendar. One issue I've encountered is the alignment of numbers within boxes, especially when dealing with double digits. When displaying a sing ...