Tips for creating distinct hover descriptions for each element in an array

My dilemma may not be fully captured by the title I've chosen, but essentially, I am working with a list of names and I want each one to display a unique description when hovered over with a mouse.

On the surface, this seems like a simple task, right?

However, here's where it gets tricky - these names are stored as elements in an array.

I can't just use < a title="DESCRIPTION" /> ARRAY CODE < /a> because that would give every name in the array the same hover description.

So, my question is: How do I go about coding it so that each name in the array triggers a different hover description?

Answer №1

If you want to connect descriptions with names, consider using an object for mapping. Here's an example:

const relationships = {
    'Bob': 'me',
    'Joe': 'my friend',
    'Bill': 'a random guy',
}

You can then iterate over the object using a for in loop to create elements. Here's how:

for (const person in relationships) {
    $('body').append(`<a title="${relationships[person]}"> ${person} </a>`);
}

Since objects have a time complexity of O(1), this method should be faster compared to using a multidimensional array. However, keep in mind that JS objects do not guarantee order. If order is important to you, consider using a map instead. Here's an example:

const relationships = new Map();
relationships.set('Bob', 'me');
relationships.set('Joe', 'my friend');
relationships.set('Bill', 'a random guy');

for (const [name, description] of relationships) {
    $('body').append(`<a title="${description}"> ${name} </a>`);
}

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

Obtain array buffer from NodeJS to capture frames from a video file

My current goal is to extract frames from a video in node without relying on a video tag. I am utilizing openvg-canvas for this task, which allows me to print images and use the canvas API functionalities successfully. The challenge lies in the fact that ...

VueJS - Validating Props with Objects

What is the best way to validate Object type props in VueJS to guarantee that certain fields are defined within the object? For instance, I need to make sure that the user prop includes 'name', 'birthDate', and other specific fields. ...

What are the capabilities of a dynamic optional form segment in web2py?

I have a web2py form that I created using SQLFORM. Within this form, there is a section that is optional and should be hidden by default. It should only be displayed when the user checks a checkbox on the form. Currently, I am hiding the different input f ...

Error encountered: A syntax error occurred due to an unexpected token ":" found in the file path D:practise odejs odejs-demoviewsindex.html

Attempting to switch the engine view from .ejs to .html resulted in an error. After searching online, I was unable to find a solution for the following problems: Express 500 SyntaxError: Unexpected token : in D:\practise\nodejs\nodejs-demo& ...

What is the correct way to iterate through a list of images fetched with getStaticProps and display them within the same component?

What is the proper way to map a list of images returned using getStaticProps? I had successfully implemented this by passing a prop to the gallery component in another page. However, I now want to consolidate all the getStaticProps code within the gallery ...

Ways to reach component method via router

When making an Ajax request and displaying the data in a table component, I encounter an issue where I need to extract the clicked data for use in another Ajax request within a different component that is called through React-Router. The challenge lies in ...

Achieving perfect alignment of an iframe on a webpage

Having an issue with aligning the iframe on my website. I have two buttons set up as onclick events that connect to internal pages displaying PHP data in tables within the iframe. Despite trying various CSS styles and positioning methods, I can't seem ...

tips on toggling div visibility based on data filtering

<script> function toggleContent(str) { if(str == 'recent' || str == 'old' && !($(".main_content").is(':visible'))) { $(".main_content").show(); } else if( str == 'newComment') { ...

Ignore linting errors in the Webpack React Fast Refresh plugin for faster performance

I have integrated the following plugin for Hot Module Replacement (HMR): https://www.npmjs.com/package/@pmmmwh/react-refresh-webpack-plugin. How do I prevent it from blocking the page display due to non-breaking errors, such as linting issues? Here is a ...

Zeroclipboard will fail to copy the text

I seem to be encountering an issue with the Zeroclipboard system. I suspect there might be an error in my code. Even though it indicates that the content has been copied, it actually hasn't been. The code I am currently using is as follows: <scri ...

I am working on a NodeJs code that utilizes (Array)Buffers and native readUInt16BE. However, since I am working within a browser context, I plan to opt for DataView.getUint16 instead

While working on adapting a JPEG parsing function in Node.js for use in a browser environment, I discovered that the original code can be found here. The challenge lies in using Node.js' Buffer class. To make it compatible with a browser environment, ...

Interaction between a main webpage and a separate popup OAuth authorization page

I need assistance with my application that includes a button to open a blank page for LinkedIn authentication. My main query involves how to notify the original page once the user has successfully completed the authentication and processing for LinkedIn. ...

Determine whether an element is currently focused using a Vue.js directive

I'm attempting to verify if an element is currently in focus, specifically an input field, and then apply a class to another element. This is the code I have been working on, but for some reason the hasFocus() function isn't functioning as expect ...

The initial child expands in width according to the content until it reaches the boundaries of the parent, triggering the

My goal is to achieve a layout where the parent container's width can vary. The first child element (red) should expand based on its content until it reaches the end of the parent, at which point text-overflow: ellipses should be applied. The second c ...

align all items centrally and customize Excel columns based on the length of the data

Is there a way to dynamically adjust the column width based on the length of data in an Excel report using PHPexcel? Additionally, how can I center all the data in the Excel sheet? Here is the current code snippet: <?php if (!isset($_POST['send&a ...

When utilizing ThreeJS video texture, it is necessary to ensure that CORS headers are included, regardless of whether the <video> tag typically requires it or

I am facing an issue with my localhost web server where a remote video from Firestore Storage is being played on a page using a <video> tag. The video plays correctly on the web page. However, I am now trying to use the same video as a texture in a T ...

Is there a way to link the scrolling of two ag-grid data tables together for synchronized movement?

I have integrated ag-grid into my Angular project and I am looking to display two data tables (ag-grid) stacked on top of each other. Both data tables should scroll together, meaning when I scroll in table 1 or table 2, the content of both tables should m ...

In the case where all children are classified as "concealed," utilize jQuery to display a <p> tag with the message "No results were found."

I'm in the process of developing a glossary page that includes filtering based on a search term. The entire functionality is being implemented using jQuery through a straightforward script. $("#txtSearch").on('keyup', function() { var s ...

Error: Property 'config' cannot be accessed because it is null

Upon transferring my Angular project from my local computer to a Linux server, I attempted to launch the project using ng serve but encountered an issue where it opened a new file in the console editor instead. After some investigation, I tried running np ...

Why does NPM not install the most recent version of the package during an update?

After installing nodemon using the command 'npm i nodemon', I always get the latest version, which is currently 2.0.2. However, if I decide to install an older version using another command, such as npm i someolderemail, and then try to update ...