Using jQuery and HTML to create numerous links that direct to a solitary iframe

I created a basic overlay setup using css, html, and jQuery. Here's how it looks:

<a class="activator" id="activator" style="" href="#">hello</a>

<div class="overlay" id="overlay" style="display:none;"></div>
<div style="display: none;" class="box" id="box">
     <a class="boxclose" id="boxclose">X</a>
      <iframe src="http://url" style="position: absolute; top: 0px; right: 0px; left: 0px; bottom: 0px;"></iframe>
</div>

     <script type="text/javascript" src="http://goo.gl/LKdBi"></script>
     <script type="text/javascript">
        $(function() {
            $('#activator').click(function(){
                $('#overlay').fadeIn('fast',function(){
                    $('#box').show('fast');
                });
            });


            $('#boxclose').click(function(){
                $('#box').hide('fast',function(){
                    $('#overlay').fadeOut('fast');
                });
            });

        });
    </script>

With this setup, when you click the link, an overlay pops up that can be closed by clicking the X.

My query is, I want to include multiple links that open their own overlays or share the same one (maybe using the same iframe). Is there a simpler way to achieve this? So far, I haven't found a solution other than making separate calls for each link.

Edit:

Ideally, I'd like to have around 5 links, each opening the overlay and displaying the URL they point to. The overlay should close when clicked away on the background or using the 'X' button.

Thank you

Answer №1

If you're in search of the nearest solutions and subsequent functions, consider utilizing classes for multiple links rather than IDs. Here's an example that could work for your situation:

$(function() {
    $('.clickable').click(function(){
        $(this).next('.popup').fadeIn('fast', function(){
            $(this).next('.content').show('fast');
        });
    });

    $('.close-button').click(function(){
        $(this).closest('.content').hide('fast',function(){
            $(this).closest('.overlay').fadeOut('fast');
        });
    });
});

In order to have just one iframe and overlay, a simplified version is provided below:

HTML:

<a class="clickable" href="http://www.example.com/">Link 1</a>
<a class="clickable" href="http://www.sample.com/">Link 2</a>

<div class="overlay" style="display:none;"></div>
<div class="content" style="display: none;">
    <a class="close-button">X</a>
    <iframe class="frame" style="position: absolute; top: 0px; right: 0px; left: 0px; bottom: 0px;> </iframe>
</div>

Javascript:

$(function() {
    $('.clickable').click(function(e){
        e.preventDefault();
        $('.frame').attr("src", this.href);

        $('.overlay').fadeIn('fast', function(){
            $('.content').show('fast');
        });
    });

    $('.close-button').click(function(){
        $('.content').hide('fast',function(){
            $('.overlay').fadeOut('fast');
        });
    });
});

Try it out here!

Answer №2

If you want to implement some cool effects, consider these CSS classes:

$(function() {
      $('.clickable').on('click', function(){
          $(this).next('.popup').fadeIn('fast', function(){
             $(this).find('.content').show('fast');
          });
      });

      $('.close-btn').on('click', function(){
          $(this).parent().hide('fast', function(){
              $(this).closest('.popup').fadeOut('fast');
          });
      });
});

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

Stylish Navigation Menu Logo/Site Name

Currently in the process of upgrading my website and I have my website name included as part of my menu, as opposed to a logo. This has been an easy solution that has worked well for me. For mobile screens, the template I purchased includes a slicknav men ...

continuously repeating with each press of the enter key

Whenever I focus on the input field and press enter, everything works fine. However, if I do it again, the action is repeated multiple times depending on how many times I focus. $(document).delegate(".changeValue","focus one",function(){ $(this ...

Choose solely the initial and concluding hyperlink within the divs

The structure I am working with is as follows: <div class="navigation_sub_item_background" id="sub_nav_2"> <div class="navigation_sub_item" id="2_1"> <a class="navigation__sub__link" href="?p=2_1"> <span> ...

I am in the process of troubleshooting why the header does not redirect correctly when clicked

As I work on developing a user authentication script for my database, I encounter an issue when trying to match the information in my MySQL database upon clicking a button. For some reason, nothing seems to happen regardless of whether the login informatio ...

What is the best way to position two divs side by side at the bottom of the

When trying to align my second div with the first one, I face an issue. If the text above the first blue box is longer, the second div appears to be outside the line of the box. Changing the relative position doesn't solve this problem either. When th ...

I can't figure out why my code isn't functioning properly. My goal is to have a PDF file generated when the submit button is clicked on a form, and for a

Hello everyone, I am new to the world of programming, but I have been assigned a project that involves using jQuery, JavaScript, html2pdf, and Bootstrap 5. I seem to be facing some issues with my code. Can someone please help me identify what's wron ...

Sending a json array from PHP

I spent several hours searching for solutions to my problem but couldn't find an answer. I'm trying to perform a search on the same page using jQuery, AJAX, and PHP. Unfortunately, the array from PHP is still returning undefined. Here is the P ...

Enhancing game menus for various mobile screen sizes

I've noticed a consistent pattern in the games I download from the Google Play Store - they all have a background image at the menu with clickable buttons. When testing these games on different devices, I found that the backgrounds didn't stretch ...

Creating a combination of associative keys and numbers within an array using JavaScript

To summarize, my question starts below: I have simply read a JSON file with the following contents: [{"FirstCategory":"Bath","SecondCategory":"Bath Accessories","ThirdCategory":""}, {"FirstCategory":"Bath","SecondCategory":"Faucets","ThirdCategory":""}, ...

Show identical modal form for various database entities

I have numerous entities that adhere to the IEntity interface (in reality, I have around 10 entities). public interface IEntity { short Id { get; set; } string Name { get; set; } } public partial class Part: IEntity { } public partial class Locali ...

The parent div isn't properly extending to accommodate the table, and the columns aren't aligning correctly within the table when using CSS flexbox

I have a scenario where I need a table inside a div to take up the entire space of the div, with the second column and its inputs expanding as much as possible to fill the width (i.e. col1+col2 = div width). Due to being nested within another div, absolut ...

Could the absence of an external style sheet for CSS be hindering the execution of my code?

A generous Stack Overflow user provided me with the code you see here and their JSFiddle can be accessed at http://jsfiddle.net/f18513hw/. The code functions properly in JSFiddle. I copied and pasted the code into my Textpad, saved it in my htdocs folder o ...

The autocomplete feature in Google Maps is not functioning properly when used within a modal

I've integrated the Google Maps API to enable autocomplete address functionality for an input box on my page. However, I'm encountering an issue where everything works fine on the page itself, but it doesn't work when the input box is placed ...

The information retrieved from the open weather map API is difficult to interpret

Currently experimenting with the Open Weather Map API. Two specific calls are being examined, one for London and another for Hermanus in South Africa. Noticing differences in the data returned from each call. Below are the two API calls: Data returned fo ...

Revolutionizing Zen Cart with slimMenu

Is there a smarter person out there who can help me understand why the slimMenu is breaking when I add these files to the bottom of the page? <link href="includes/templates/westminster_new/css/age-verification.css" rel="stylesheet"> <script src=" ...

Troubleshooting issue: Asynchronous functionality not working with Ajax.BeginForm

Struggling to grasp ASP.Net MVC and facing challenges with using Ajax.BeginForm to update a partial view asynchronously. Here's the code snippet in the view for the action: @using (Ajax.BeginForm( new AjaxOptions { ...

Having issues with jQuery not functioning on this specific web page across all browsers. The reason behind this puzzling dilemma remains elusive to me

I am experiencing issues with the functionality of my website. The buttons with + signs are meant to be drop-down toggles, and the mini-tabs below them should work as tabs. Additionally, the sample images at the bottom are not loading properly when clicked ...

What could be the reason behind a CSS caption overlapping another image within this table?

This is the CSS caption effect I am using: .cell { position: relative; width: 180px; height: 180px; } .caption {} .cell .caption { opacity: 0; text-decoration-color: gray; text-align: center; background: #eaeaea; position: absolute; fo ...

Refresh the datatable automatically when a field is updated without the need for AJAX requests

I need assistance with sorting a datatable column that contains checkboxes. The issue I'm facing is that the sorting is not working due to me changing the value of the column. Since it's not generated via ajax and I can't use the reload() me ...

Gather information from "div" tag with the class attribute using Python

I am looking to extract specific financial data (referred to as the Key data) that is housed within <div> </div> tags. I then aim to consolidate this extracted information into an excel sheet which already contains additional harvested data. Th ...