Second dialog element not working and I don't know why

Hello I am trying to make modal images with the <dialog> element and I can only get one singular image to work with it. Any idea why? https://codepen.io/davesamuels1998/pen/MYgzxrY
38 Replies
clevermissfox
clevermissfox4w ago
You have two dialogs with the same class and two buttons with the same class , you arent doing anything to differentiate between and identify which is which so it only works for the first one. you need to use querySelectorAll() and loop through all instances for all cases https://codepen.io/Miss-Fox/pen/MYgZbeV?editors=0010
ἔρως
ἔρως4w ago
you can use a single dialog for both images if you want, you can even clone the image to the dialog, saving you a lot of work
Takyon_is_Online
Takyon_is_OnlineOP4w ago
I would love to do this however this is really for my website and it has content in between each image. It's the one I showed you before. Would a single dialog still be possible? I have edited the codepen to have content between the images. Ok I will try that now thanks!
ἔρως
ἔρως4w ago
yes, the exact same way i described
Takyon_is_Online
Takyon_is_OnlineOP4w ago
I figured out how to do it thanks!
ἔρως
ἔρως4w ago
how did you do it?
Takyon_is_Online
Takyon_is_OnlineOP4w ago
I don't know much Javascript at all so I asked an AI to refactor what I had to add the feature for me
// Select the single dialog and its elements
const dialog = document.getElementById("image-dialog");
const dialogImg = dialog.querySelector(".dialog-img");
const cancelBtn = dialog.querySelector(".cancel");

// Attach click event listener to the container
document.body.addEventListener("click", (event) => {
const button = event.target.closest(".updateDetails");

if (button) {
// Get the image inside the clicked button
const clickedImage = button.querySelector(".card__img");

if (clickedImage) {
// Set the source and alt attributes of the dialog image
dialogImg.src = clickedImage.src;
dialogImg.alt = clickedImage.alt;

// Show the dialog
dialog.showModal();
}
}
});

// Close the dialog on cancel button click
cancelBtn.addEventListener("click", () => {
dialog.close();
});
// Select the single dialog and its elements
const dialog = document.getElementById("image-dialog");
const dialogImg = dialog.querySelector(".dialog-img");
const cancelBtn = dialog.querySelector(".cancel");

// Attach click event listener to the container
document.body.addEventListener("click", (event) => {
const button = event.target.closest(".updateDetails");

if (button) {
// Get the image inside the clicked button
const clickedImage = button.querySelector(".card__img");

if (clickedImage) {
// Set the source and alt attributes of the dialog image
dialogImg.src = clickedImage.src;
dialogImg.alt = clickedImage.alt;

// Show the dialog
dialog.showModal();
}
}
});

// Close the dialog on cancel button click
cancelBtn.addEventListener("click", () => {
dialog.close();
});
ἔρως
ἔρως4w ago
that code is vile but, if it works...
Takyon_is_Online
Takyon_is_OnlineOP4w ago
I don't know how to do it myself, also I don't know if you would have made it for me. I will revisit this some day, but now I just want to write blogs about IT stuff.
ἔρως
ἔρως4w ago
i mean, it is vile but works and in the end, that's what matters
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Yeah lol, I don't know if it works that good though because I have a problem with the image not being centered know even though that's the default function of the <dialog> tag. It works in my codepen perfectly though so I am confused about that. I don't think setting a max-width or max-height for my image would mess with default positioning of <dialog> would it?
ἔρως
ἔρως4w ago
no, but setting some styles can ruin the dialog
Takyon_is_Online
Takyon_is_OnlineOP4w ago
I honestly don't know where I can point too. I did style my image inside the dialog but it was only this
.dialog-img {
width: 100%;
height: 100%;
object-fit: contain;
}
.dialog-img {
width: 100%;
height: 100%;
object-fit: contain;
}
I had the problem before I added this code, or the vile Javascript. I've changed my code so much that I don't know what could have caused it. https://github.com/DaveSamuels1998/Dave-Samuels-Blog
GitHub
GitHub - DaveSamuels1998/Dave-Samuels-Blog
Contribute to DaveSamuels1998/Dave-Samuels-Blog development by creating an account on GitHub.
ἔρως
ἔρως4w ago
honestly, i wouldnt add any styles to the image just a max-width of 100% thats it
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Alright I fixed it. Also I think my images don't appear in the middle because I resized some of them using a image editing software. So I made them smaller manually. I'm just guessing lol but I think maybe that's it
ἔρως
ἔρως4w ago
i would need to see the actual code to tell you
Takyon_is_Online
Takyon_is_OnlineOP4w ago
How should I do that? It's a lot of code. Would you be able to find it in my Github?
ἔρως
ἔρως4w ago
simplify it until the bug doesnt happen then add back some code do this until you find what's causing it or you can't find it
clevermissfox
clevermissfox4w ago
Do you understand now why it wasn’t working ? Also made some comments/notes in the JavaScript to hopefully help your learning
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Well not really lol since I haven't practiced Javascript in years and I only had the basics understanding when I did. I had a AI write that code for me so I did skim through the explaination already. My main goal is to get a website to use, I will come back to this once I go through the frontend developer challenge from beginner to intermediate. This is just way too much for me to comprehend fully. I think I was way over my head trying to add certain functionalities to this website. I originally got this website from following a code along. I found the code that makes the modal not centered .flow > *:where(:not(:first-child)) { margin-top: var(--flow-spacer, 1em); } I commented this out and the modal when back to normal. I want to keep this code but not have it affect dialog elements when they are open. I'm still trying to figure that part out but failing.
ἔρως
ἔρως4w ago
dialogs should be in the <body> also, you have a tag called <flow>? or is it supposed to be a class?
Takyon_is_Online
Takyon_is_OnlineOP4w ago
It's a class Also I have the dialog element in my nunjucks file but it should still be in my body tag once eleventy generates the html. I have to check again
ἔρως
ἔρως4w ago
does the body tag have the .flow class?
Takyon_is_Online
Takyon_is_OnlineOP4w ago
I'll go to my laptop and check
Takyon_is_Online
Takyon_is_OnlineOP4w ago
No it's a div that has the .flow class. This is how the div looks when I highlight it in developer tools
No description
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Everything is inside an <article> tag which is inside the body tag
Takyon_is_Online
Takyon_is_OnlineOP4w ago
No description
ἔρως
ἔρως4w ago
the dialog should be inside the <body>, not in a <div>
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Ok, I wasn't aware that could effect certain attributes. I will edit that once I get back home. Thanks!
clevermissfox
clevermissfox4w ago
There’samy reasons why you shouldn’t have AI spit code out when you cannot verify it does what it’s supposed to (by reading it and understanding ) and this whole thing is a great example of one of those reasons. Instead of using the time googling and looking on MDN and learning , you had AI spit out some code, the probably wrestled with it for a bit, and now you’re needing help from real people because the AI didn’t give you what you wanted. This could have been a great opportunity for you to learn what these methods do and how to handle having two or more of the same elements/classes with the same behaviour. I think it’s a much better plan to address once you’ve gone through the tutorials and have a better understanding. AI usually makes more of a mess than it helps; it’s given me the most inane hallucinations , now I just use it to comment up files or refactor what I’ve already written so I can learn or be reminded of a more succinct way. Had I known that you didn’t write this yourself and wouldn’t understand the changes , I wouldn’t have made a codepen fixing it tbh. What’s the point if you’re not going to learn from it. Anyways good luck in your courses and once your finished you’ll come back to this challenge knowing exactly what you need to do and why the original code didn’t work
ἔρως
ἔρως4w ago
for css, a dialog isn't special in any way, and what you throw at it will affect it a quick and easy way to avoid issues is to always have the <dialog> in the <body>
Takyon_is_Online
Takyon_is_OnlineOP4w ago
You’re right, in my mind I was thinking that I wasn’t proficient in CSS so I wouldn’t stand a chance learning this in Javascript yet. It’s the wrong way to look at it but that was my thought process. I wanted to finish this website then start over with the basics and work my way up. Since this website was already built from me doing a code along I decided to just use and outsource what I didn’t know and come back to it later or build the whole thing from scratch because honestly I don’t understand a single thing from eleventy.js or the nunjucks part. Kevin Powell just showed it off and didn’t explain all those stuff because he was just really showing the website off. Not blaming him but I just figured I would just use AI to help me do the parts I didn’t understand because this all isn’t my code. I just copied it from Kevin It worked, now I know not to do that anymore. Thanks for sticking it out with me, I truly appreciate it!
ἔρως
ἔρως4w ago
you're welcome the problem with ai is that it tends to generate absolute garbage at a certain point also, if it isn't in the dataset, ai can't generate it, but it will try it's damn hardest (hallucinate) if you don't know anything, you ask chatgpt for code, and it doesn't work, then you're just trying to polish a 💩 it's hard to fix human code, now imagine ai that hallucinates garbage chatgpt will even gaslight you into accepting it as the right answer, because that's what it is for
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Thanks for putting it into perspective even though I was coming to that conclusion. If you don't know anything and you ask AI to code it then it will give you nonsense that don't work and you don't know why which is a horrible feeling. I won't be using chatGPT like that ever again. Time to start the grind on frontend mentor.
ἔρως
ἔρως4w ago
there's a good reason i don't give you the code and let you do the bad code and fail: you learn by the mistakes if you have an infinite code generator, you won't learn shit the grind and failing and the errors are important part of learning
Takyon_is_Online
Takyon_is_OnlineOP4w ago
Yeah that’s true. I always thought of the failing part as a headache but there is always a chance to learn. I need to improve on my mindset in that department
ἔρως
ἔρως4w ago
dude, i fail everyday just today, i had to do 5 revisions because i added small little bugs which i found out with extensive testing

Did you find this page helpful?