Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Multiple Modals on One Page

cmcp

Coder
I've just learned how to make modal boxes using HTML, CSS, and Javascript. Making one modal box isn't very hard for me at all. My problem is creating multiple modal boxes for the same page. The page I'm building now needs 3 different modals (the look is exactly the same, but the text needs to be different). So far, only the first one is appearing on the page. How can I get all 3 of them to appear when their individual buttons are pressed? I've tried renaming the classes but it's not working.

Please help.
 
Solution
Here is an example of 3 modals on a page:


A couple other notes:

Please note the HTML changes. You should strive to use the most appropriate HTML element in each situation. For example, wrapping your "button" image in an actual button HTML element (style appropriately via CSS) and, equally important, giving the img some appropriate alt text.

Also, the <font> element is obsolete. See this image:
from here: HTML element: font | Can I use... Support tables for HTML5, CSS3, etc

Use inline styles instead, if you must, on the appropriate element. Better yet, add CSS to appropriate elements via an external style sheet or embedded in the <head> of your...
Couple things might work here. You could have the content dynamic based on which button is clicked. I won't get into how as I have no idea what you are building or with what. I'm sure JS could be used. Off the top of my head, I'd say the issue is that the 3 modals are referenced via IDs and all 3 modals currently have the same ID.
 
Here is an example of 3 modals on a page:


A couple other notes:

Please note the HTML changes. You should strive to use the most appropriate HTML element in each situation. For example, wrapping your "button" image in an actual button HTML element (style appropriately via CSS) and, equally important, giving the img some appropriate alt text.

Also, the <font> element is obsolete. See this image:
from here: HTML element: font | Can I use... Support tables for HTML5, CSS3, etc

Use inline styles instead, if you must, on the appropriate element. Better yet, add CSS to appropriate elements via an external style sheet or embedded in the <head> of your HTML.

I also highly recommend the site caniuse.com. Very helpful for feature support.

Let me know if you have any other questions!
 
Last edited:
Solution
Here is an example of 3 modals on a page:


A couple other notes:

Please note the HTML changes. You should strive to use the most appropriate HTML element in each situation. For example, wrapping your "button" image in an actual button HTML element (style appropriately via CSS) and, equally important, giving the img some appropriate alt text.

Also, the <font> element is obsolete. See this image:
from here: HTML element: font | Can I use... Support tables for HTML5, CSS3, etc

Use inline styles instead, if you must, on the appropriate element. Better yet, add CSS to appropriate elements via an external style sheet or embedded in the <head> of your HTML.

I also highly recommend the site caniuse.com. Very helpful for feature support.

Let me know if you have any other questions!

Thank you SO much for your help! I had been struggling with this issue and driving myself completely batty for quite a while trying to figure things out. I have one last question for you regarding the script. I've noticed that you used the for each method for the modals. Would I be correct in assuming that if I were to add more modals that the JS script would work as is and not have to be changed? I'm just asking for future reference. Thanks again for the help. I really appreciate all you've done.

cmcp
 
Thank you SO much for your help! I had been struggling with this issue and driving myself completely batty for quite a while trying to figure things out. I have one last question for you regarding the script. I've noticed that you used the for each method for the modals. Would I be correct in assuming that if I were to add more modals that the JS script would work as is and not have to be changed? I'm just asking for future reference. Thanks again for the help. I really appreciate all you've done.

cmcp
You are correct, as long as the additional HTML modal elements have the necessary 'data-modal' attributes and the classes needed by the script.
 
Back
Top Bottom