Popup Image Problem, Need Help

I followed a tutorial from: Popup with blurred background Using CSS3 And Vanilla Javascript | Modal* (This is the title of the video).

The thing is that I made it that when you click the image itself then the image will popup like the text from the video.

The problem is that for me since I’m trying to popup an image the rest of the images popup in the bottom as well. I have several images horizontally and when I click on one I want only that one to popup.

Here is the code:

>
        <div class="content2" id="blur">  <div class="pBox">
        <a href="#" class="anchor" onclick="toggle()">
            <img src="../mywebsite/img/icon1.jpg">
            <div class="overlay">
                <h2>Web Design</h2>
            </div>
        </a>
        </div>
        <div class="pBox">
        <a href="#" class="anchor" onclick="toggle()">
            <img src="../mywebsite/img/icon2.jpg">
            <div class="overlay">
            <h2>Graphics Design</h2>
            </div>
        </a> </div>

>
       <div id="popup"> <a href="#" class="anchor" onclick="toggle()">
        <img src="../mywebsite/img/icon1.jpg">
        <img src="../mywebsite/img/icon2.jpg">
        </a>
    </div>

> .content2{
	position: relative;
	display: flex;
	justify-content: space-between;
}

> .content2#blur.active{
	filter: blur(20px);
	pointer-events: none;
	user-select: none;
}

> #popup{
	position: fixed;
	top: 40%;
	left: 50%;
	transform: translate(-50%, -50%);
	width: 600px;
	padding: 50px;
	visibility: hidden;
	opacity: 0;
	transition: 0.5s;
}

> #popup.active{
	width: 45%;
	top: 50%;
	visibility: visible;
	opacity: 1;
	transition: 0.5s;
}

Javascript:

>
	function toggle(){
	var blur = document.getElementById('blur');
	blur.classList.toggle('active');
	var popup = document.getElementById('popup');
	popup.classList.toggle('active');
}

You have’nt shown your toggle function yet. So I cannot be 100% correct with my answer but no harm in trying…
You may have toggled all the images except the specific one.

You can do…

Let images = document.querySelectorAll('img');
For(let image in images){
  If(image == ""your image to popup"")
   Image.style.visibility='visible';
 else 
image.style.visibility='hidden';
}

There’s a lot of typos in there, but even without them, I don’t think this would work. You’re hiding all the images instead of the popup(s).

@masusaca Can you share your JS code?

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

function toggle(){
var blur = document.getElementById(‘blur’);
blur.classList.toggle(‘active’);
var popup = document.getElementById(‘popup’);
popup.classList.toggle(‘active’);
}

Thank you! I’m new and was having trouble with it.

function toggle(){
	var blur = document.getElementById('blur');
	blur.classList.toggle('active');
	var popup = document.getElementById('popup');
	popup.classList.toggle('active');
}

It would be much easier to help if you’d provide the full code (via https://codepen.io for example), because it’s hard to tell what exactly is your error. You have two images in your popup, do you want only one to show up?

Yes! I created a list of images. When you click on one of them only one should popup, but both images do. I want to fix that and don’t know how.


The codes in the link above don’t work that well compared to mine. But, as you can see there are two images horizontally. When I click on one, the background turns blurry and the image I clicked should popup. Instead, all images popup.

First, your <div class="content"> has no closing tag. It’s unclear where that div should close, but if I edit the code like this:

<section class="p">
  <div class="content2" id="blur">  <div class="pBox">
        <a href="#" class="anchor" onclick="toggle()">
            <img src="../mywebsite/img/icon1.jpg">
            <div class="overlay">
                <h2>Web Design</h2>
            </div>
        </a>
        </div>
        <div class="pBox">
        <a href="#" class="anchor" onclick="toggle()">
            <img src="../mywebsite/img/icon2.jpg">
            <div class="overlay">
            <h2>Graphics Design</h2>
            </div>
        </a> 
   </div>

</div> <!-- inserted the closing tag here-->

   <div id="popup"> 
         <a href="#k" class="anchor" onclick="toggle()">
        <img src="../mywebsite/img/icon1.jpg">
        <img src="../mywebsite/img/icon2.jpg">
        </a>
   </div>
    </section>

at least something pops up, but it’s difficult to tell what’s popping up because all the image links are broken.

If you want only one of the two images in your <div id="popup"> to show, you can’t put them in the same popup-container and then toggle the active class on your whole container with both images inside. Of course they’ll both show then.

I’d rather have two popup-containers and then toggle them individually, depending on what was clicked before.

Yes, that’s where the div goes. I forgot to copy that one.
As for the toggle do you mean having two different functions or just a toggle div for each images?

I tried putting the images in different links, for example:

<div id="popup"> 
         <a href="#k" class="anchor" onclick="toggle()">
        <img src="../mywebsite/img/icon1.jpg">
        </a>
        <a href="#k" class="anchor" onclick="toggle()">
        <img src="../mywebsite/img/icon2.jpg">
        </a>     
   </div>

This does the same thing.

You only need one function and one popup, but you need something that tells the function which image it should show and which it should hide. You’ll first have to give your links an id as an easily accessible identifier, like this:

<div class="content2" id="blur"> 
    <div class="pBox">
        <a href="#" class="anchor" id="img-1" onclick="toggle()">
            <img src="../mywebsite/img/icon1.jpg">
            <div class="overlay">
                <h2>Web Design</h2>
            </div>
        </a>
    </div>
   </div>
  </div>

The other <a> tag gets the id="img-2".
(It doesn’t have to be img-1 and img-2, can be any name you choose)

Then in your popup, you give the same names to your images, but this time as class attribute:

<div id="popup"> 
         <a href="#k" class="anchor" onclick="toggle()">
        <img class="img-1" src="../mywebsite/img/icon1.jpg">
        <img class="img-2" src="../mywebsite/img/icon2.jpg">
        </a>
   </div>

Your toggle function will now read the id of the link, loop over all images in the popup and set display:none on all of them, except the one with the class name you want to show:

function toggle(){
	var blur = document.getElementById('blur');
	blur.classList.toggle('active');
	var popup = document.getElementById('popup');
	popup.classList.toggle('active');
  
  let imgClass = event.target.closest('a').id; // reads the id from the <a> tag
  Array.from(popup.querySelectorAll('img')).forEach(image => { // loops over all images with forEach
    if (image.classList.contains(imgClass)){ // if it's the image belonging to the <a> tag that was clicked,
      image.style.display = 'block' // set display to 'block'
    } else {
      image.style.display = 'none' // set display to 'none' on all other images
    }
  })
}

As you only have two images right now, the function doesn’t really need that loop, but this way, you can add as many images and links as you like and it’ll always work.

1 Like

Holy cow! That actually works. Could you please explain me a bit about this new function (codes) you wrote to make it work? I only know the basics of JavaScript and nothing this difficult (in my opinion).

One more thing. As you can see the other images turn blurry when I click on one of them. I have a footer. How can I also make it blurry? I tried adding the id=“blur” but it only works for one div and not for the rest (that has the same id tag).
To be more direct, I wanted to disable scrolling when you popup and image, but be able to close it back by just clicking at it. I can’t find anything easy. Everything about it seems a bit complicated.

I added comments to make it clear what each line does, I could only repeat those now. You might want to surf through the basics of JavaScript in the curriculum, it’ll give you all the skills you need for stuff like this.

I don’t really understand what you mean with the second paragraph, but as for the blur thing - instead of giving your elements an id blur, you could give them a class blur, and then modify your toggle function like this:

function toggle(){
	var blurElements = Array.from(document.getElementsByClassName('blur')); // gets all elements with class "blur"
    blurElements.forEach(element => element.classList.toggle('active')); // loops through all elements and toggles class "active" on them
  })
}
1 Like

Thank you for the help. Sadly it didn’t work.

Have you given all the elements you’d like to blur a class="blur"? You might run into trouble if an element already has another class. If you have an updated codepen, it would be easier to see where the problem is.

Yes I did. I changed them to class, but that didn’t do anything at all even when I click on the image it didn’t popup anything and nothing turned blurry.

Can you provide another codepen? Because otherwise I could only wildly guess why it doesn’t work.

Let me do it really quick.
Link for code: https://codepen.io/masusaca/pen/LYZZogO

A few small issues to fix:

  1. This won’t work: <div class="footer" class="blur">
    An element can only have one class attribute, so you’ll have to write them like this instead:
    <div class="footer blur">, also the other one:
    <div class="content2 blur">

  2. You gave your popup the blur class, too - that actually breaks things, so just remove the class from there

  3. You forgot to add the img-classes in your popup, that’s why they’re currently both set to display:none
    <img class="img-1" src="https://www.w3schools.com/images/lamp.jpg">
    <img class="img-2" src="https://www.w3schools.com/images/lamp.jpg">

And here’s the script, I’ve simplified it a bit in case you’d like to try to understand what it does:

function toggle(){
	let blurElements = document.querySelectorAll('.blur');
	blurElements.forEach(element => element.classList.toggle('active'));

	let popup = document.querySelector('#popup');
	popup.classList.toggle('active');

    let imgClass = event.target.closest('a').id;
    popup.querySelectorAll('img').forEach(image => {
      if (image.classList.contains(imgClass)){
        image.style.display = 'block'
      } else {
        image.style.display = 'none'
      }
    })
}
1 Like