Don't waste time creating multiple popups for changing content - streamline your process

Challenge

I've successfully extracted information from an array and displayed it dynamically in a tooltip/popup window above each photo on the page. However, with 56 different individuals at various locations, arranging them neatly in a grid poses a challenge.

Inquiry

a) Is there a way to streamline the creation of tooltips for each person so that clicking on their image will display the tooltip with relevant dynamic content?

scripts.js

    $(function() {

        // MLAs
        var MLAs = [
          {
            "Name": "Nancy Allan",
            "Age": 62,
            "Constuency": "St. Vital",
            "Party": "NDP",
            "Gender": "Female",
            "Ethnicity": "White"
          },
          {
            "Name": "James Allum",
            "Age": null,
            "Constuency": "Fort Garry-Riverview",
            "Party": "NDP",
            "Gender": "Male",
            "Ethnicity": "White"
          },
          {
            "Name": "Rob Altemeyer",
            "Age": null,
            "Constuency": "Wolseley",
            "Party": "NDP",
            "Gender": "Male",
            "Ethnicity": "White"
          }]

        // Display popup with MLA info
        $(".headshot").click(function(){
            var idx = $(this).index() - 1;

            $(".tooltip").fadeIn("slow");
            $(".tooltipName").html(MLAs[idx].Name);
            $(".tooltipParty").html(MLAs[idx].Party);
            $(".tooltipConstuency").html(MLAs[idx].Constuency);
            $(".tooltipEthnicity").html(MLAs[idx].Ethnicity) + ",";
            $(".tooltipAge").html(MLAs[idx].Age);
        });

    // Positioning of tooltips
    $('.headshot').each(function(){
        var img = $(this);

        img.click(function(){
            $('.tooltip')
            .show(100)
            .text(img.attr('alt'))
            .offset({
                top : img.offset().top + img.height(),
                left : img.offset().left
            });
        });
    });
 });

index.html

            <div class="tooltip">
                <div class="info">
                    <p class="tooltipName"></p>
                    <p class="tooltipParty"></p> <p class="tooltipConstuency"></p>
                    <p class="tooltipEthnicity"></p> <p class="tooltipAge"></p>
                    </div><!-- /.info -->

                    <div class="arrow-down">
                    </div><!-- /.arrow-down -->
                </div><!-- /.tooltip -->

<img src="assets/img/headshots/allan.jpg" alt="" id="0" class="headshot NDP Female White">
                <img src="assets/img/headshots/allum.jpg" alt="" id="1" class="headshot NDP Male White">
                <img src="assets/img/headshots/altemeyer.jpg" alt="" id="2" class="headshot NDP Male White">

tooltip.scss

/*----------------------------------
TOOLTIP
----------------------------------*/

.tooltip {
    display: none;
    position: relative;
    left: -12px;
    top: -5px;
}

.info {
    @include serifLight;
    background: $yellow;
    color: $black;
    font-size: 1.4rem;
    padding: 10px;
    width: 9%;
    text-align: center;

    p {
        margin: 0px;
    }
}

.tooltipName, {
    font-family: 'Roboto Slab',serif;
    font-weight: 700;
}

.tooltipEthnicity, .tooltipAge {
    display: inline;
}

.arrow-down {
    width: 0;
    height: 0;
    border-left: 15px solid transparent;
    border-right: 15px solid transparent;
    border-top: 20px solid $yellow;
    position: relative;
    left: 36px;
}

Answer №1

If you want to customize the position of the tooltip, you can utilize the .offset() values.

Check out this example on JSFiddle

$('img').each(function(){
    var img = $(this);

    img.click(function(){
             $('.tooltip')
             .show(100)
             .text(img.attr('alt'))
             .offset({
                top : img.offset().top + img.height(),
                left : img.offset().left
        });
    });
});

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

Typescript enhances Solid JS by using the "as" prop and the "component" prop

Hey there, I've been experimenting with solid-js lately and I'm facing a challenge integrating it with typescript. My objective is to make my styling more modular by incorporating it within my components. type RelevantTags = Exclude<keyof ...

CSS, Assistance in incorporating a sub menu into a drop-down menu

I'm looking to enhance my drop down menu by adding a sub menu. Specifically, I would like to create a sub menu for Item 3 in the HTML below. How can I achieve this? Thank you, Below is my CSS code: .nav_menu { width:100%; background-color:# ...

Dragging objects on a map is done using jQuery and it causes them to bounce

Currently, I am working on implementing the JQuery Draggable feature. My idea is to create a map where I can attach image Divs to it dynamically using Jquery. So far, I have been successful in adding the Divs and making them draggable around the map effici ...

Expanding iframe elements

I am having difficulty adjusting the size of the iframe content (within a fixed-sized iframe). Essentially, I would like the content to scale smaller or larger as if the browser's zoom function was being used. Through my CSS experiments, it seems achi ...

Request the generic password prior to revealing the concealed div

I want to implement a feature where a hidden div is shown once a user enters a password. The password doesn't need to be stored in a database, it can be something simple like 'testpass' for now. Currently, I have written some code using Java ...

The best way to display a jquery dropdown menu using selenium in a django test

I am having trouble displaying a jQuery dropdown menu in a Django test using Selenium. Here are some snippets of the code ("admin_user" is the link that should reveal the dropdown menu with the link "Coop Admin App"): def wait_loading(self, driver, xpath_ ...

Exploring the World of 2D Array Animation

I'm in the process of creating a Pacman ghost using visual 2D arrays. I would like the ghost to move similar to this: https://i.sstatic.net/KPmUt.gif I am considering implementing CSS transitions for the movement, but I'm unsure about how to do ...

The functionality of Nativescript squared buttons is not functioning as expected

I recently developed a Nativescript app using Angular, incorporating customized buttons. However, I have encountered an issue where I am unable to achieve perfectly squared buttons with the desired appearance. Here is my CSS code: Button { font-size: ...

Using Laravel 4 for dynamic pagination via AJAX requests

My controller has a function that is supposed to return a paginated set of items. I am using the same method as in all my other controllers (where it works perfectly), but for some reason, I am getting back an empty object. Here is the code for the functio ...

Modifying the CSS properties of elements with identical IDs

Encountering an issue while attempting to modify the CSS property of elements with similar IDs using JavaScript. Currently, running a PHP loop through hidden elements with IDs such as imgstat_1, imgstat_2, and so forth. Here is the code snippet being emp ...

Is it possible for a Bootstrap modal dialog to overlay another dialog window?

<button class="btn" onClick="$('#firstModal').modal('show');">Click Here</button> <!-- Modal --> <div id="firstModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden=" ...

What is the reason for Vue not updating the component after the Pinia state is modified (when deleting an object from an Array)?

When using my deleteHandler function in pinia, I noticed an issue where the users array was not being re-rendered even though the state changed in vue devtools. Interestingly, if I modify values within the array instead of deleting an object from it, Vue ...

I plan to add new information to my table only when the user clicks on the submit button. This is what I have accomplished until now

<!DOCTYPE html> <html> <head> <title>Sign up page</title> </head> <body> <form method="get" action="signup_redirect.php"> username: <input type="text" name="username" placeholder=" ...

Utilizing React to dynamically load JSON data and render a component

I am currently facing a challenge in rendering a React component that includes data fetched from a JSON using the fetch() method. Although the API call is successful, I am experiencing difficulties in displaying the retrieved data. Below is the code snip ...

Troubleshooting my CSS navigation bar - What am I missing?

I've been developing a navigation bar using a combination of HTML, CSS, and JavaScript. After writing the code and setting the display of the bar to fixed, I encountered an issue where the content of the page was overlapping the nav bar. Below you&ap ...

Modifying the overflow behavior within a Vuetify dialog

Is there a way to have a floating action button positioned on the top right of a dialog, slightly overflowing so it's offset from the dialog itself? I'm struggling to override the 'overflow-y: hidden' property set by v-dialog. <v-dia ...

Accept JSON data in ASP.NET MVC action method for posting data

I have a model class named Parcel which contains the parameters Name and CenterPoint: public class Parcel { public string Name { get; set; } public object CenterPoint { get; set; } } The values for these parameters are obtained from a map. When a ...

Increase the value of a number using jQuery

When using jquery to animate a random number increment from 0001 to 1000, the issue arises when reaching the number 0077. The final number displayed is 77 instead of 0077. Is there a solution to ensure the format remains as 0077? Your help is appreciated. ...

generating dynamically evolving controls within the MVC framework

I am looking to dynamically create controls in a view based on the source. For example, if the type is a text box, I want to generate a text box; if it is a check box, I want to create a check box dynamically in MVC. Below is the snippet of my current code ...

When utilizing AngularJS, a parse error is triggered by array[ {{value}} ], but I prefer to overlook it

Within the controller, I have a selection list that is displayed like this: <select class="input form-control" id="animationTime" ng-options="item as item.label for item in aniCon.timeOptions track by item.id" ng-model="aniCon.popupTime" ...