Siblings are equipped with the advanced selector feature to

I'm struggling to understand this setup I have:

<div class="container">
     for n = 0 to ...
         <a href="some url">Link n</a>
     endfor

     for each link in ".container"
          <div class="poptip"></div>
     endfor
</div>

For example:

<div class="container">
     <a href="some url">Link 1</a>
     <a href="some url">Link 2</a>
     <a href="some url">Link 3</a>

     <div class="poptip">Some content related to link 1 retrieved with ajax</div>
     <div class="poptip">...</div>
     <div class="poptip">...</div>
</div>

The challenge is trying to display the .poptip on hover over the anchor tag. It works fine with one link, but not with multiple links. The current CSS styling that is not working in cases with >1 link:

.producttooltip {
  position: relative;
}
.producttooltip a:hover + div {
  display: block;
}

I can't change the HTML structure, it will always be container > all links followed by all poptips. I can add unique identifiers to anchor tags and poptips, but I'm unsure how to select them in CSS:

a:hover + div[rel=a.rel] {
    display: block
}

So my question is, can I achieve this purely using CSS or do I need to use JavaScript? Ideally, I'd like to stick with CSS if possible.

Edit: I can't alter the HTML structure. The most straightforward solution would involve wrapping each element with its corresponding poptip, but I'm unable to do so.

Answer โ„–1

If you're looking to achieve this effect, here's a simple way to do it:

$('a').on('hover', function() {
  $('.poptip').eq($(this).index()).show();
}, function() {
  $('.poptip:visible').hide();
});

Using only CSS for this can be a bit tricky. However, I've included a CSS solution below as well. Feel free to check it out if you're interested in a CSS-only approach.


You can actually accomplish this using just CSS. Despite the availability of numerous plugins, let's create something like this from scratch. Firstly, you'll need an element that triggers the hover effect, like a link.

<a href="#">Hover Over Me!</a>

Next, we'll add the tooltip itself. For now, let's use a <span> element and place it inside the link.

<a href="#">Hover Over Me!<span class="tooltip">Hello there!</span></a>

Now onto the CSS part:

a span {display: none; position: absolute; color: #fff; background: #000; padding: 5px;}
a {position: relative;}
a:hover span {display: block; text-align: center;}

Code Snippet

a span {display: none; position: absolute; color: #fff; background: #000; padding: 5px;}
a {position: relative;}
a:hover span {display: block; text-align: center;}
<a href="#">Hover Over Me!<span class="tooltip">Hello there!</span></a>

This is just one example of achieving a pure CSS solution. You can view a live demonstration on JSFiddle here.


While there are plenty of plugins available that build upon this concept for creating hover-cards and tooltips. Some popular examples include:

Answer โ„–2

Using jQuery for Element Display

To make certain elements appear when hovering over specific links, you can utilize the mouseenter/mouseleave event.

$('a').on('mouseenter', function() {
  var i = $(this).index();
  $('.poptip').eq(i).show();
}).on('mouseleave', function() {
  $('.poptip').hide();
});
.poptip { 
  width:100%;
  float:left;
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
     <a href="some url">Link 1</a>
     <a href="some url">Link 2</a>
     <a href="some url">Link 3</a>

     <div class="poptip">Some content related to link 1 retreived with ajax</div>
     <div class="poptip">Some content related to link 2 retreived with ajax</div>
     <div class="poptip">Some content related to link 3 retreived with ajax</div>
</div>

Answer โ„–3

To accomplish this using jquery, simply retrieve the index of the current anchor element and display the corresponding div located at that index.

HTML SNIPPET:

<div class="container">
  <a href="some url">Link 1</a>
  <a href="some url">Link 2</a>
  <a href="some url">Link 3</a>

  <div class="poptip">Some content related to link 1 retrieved with ajax</div>
  <div class="poptip">...</div>
  <div class="poptip">...</div>
</div>

JAVASCRIPT SNIPPET:

$(function(){
   $('a').on('hover',function(){
      var ind = $('a').index(this);
      $('.poptip').eq(ind).css('display','block');
   });
});

Check out the Live Demo on JSFiddle

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

Switch out the designated text with HTML using jQuery

Currently, I have a collection of specific elements that were gathered using the following snippet: $(selector).nextUntil('tr.some_class'); My goal is to completely replace the existing content with fresh HTML code. Although jQuery provides re ...

Basic Inquiry: Unhandled TypeError - Unable to access the property 'length' of an object that is not defined

Currently, I am utilizing a straightforward JSON Ajax request to fetch some JSON data. However, every time I attempt to use the JSON object, I encounter the following error: Uncaught TypeError: Cannot read property 'length' of undefined $(do ...

The function getAttribute for isPermaLink is returning a null value when accessing an element within an RSS feed using

Struggling to retrieve the value of the isPermaLink attribute using protractor, I have attempted various methods. While successful in fetching values from other elements, the isPermaLink always returns null. HTML <guid isPermaLink="false">public-a ...

Vue.js does not apply the "selected" attribute to set the HTML <option> tag to default

Trying to designate a default <option> tag from the available options. Utilizing v-model on the <select> tag, the vue.js documentation indicates the following approach: v-model will disregard the ... selected attributes present on form ele ...

How can you ensure that a row of content is accessible by making it clickable in a valid way?

I'm currently working on developing a widget that resembles a clickable bootstrap media object list that is easily accessible. https://getbootstrap.com/docs/4.3/components/media-object/#media-list The original code has the JavaScript click event attac ...

How do I go about updating my code for welcome messages from discord.js v12 to v13?

While watching a YouTube tutorial on welcome messages, I decided to copy the entire code. However, when I tried using this code with discord.js v13, it didn't work. Strangely enough, everything seemed to function perfectly fine with discord.js v12. In ...

How can I represent non-ASCII characters in Java Script using encoding?

Imagine if ch = รก the desired result is = \u00e1 however the current output = %E1 when escape(ch) is used and current output = %C3%A1 when encodeURIComponent(ch) is used I am working with an API that supports Unicode characters. ...

The API results are able to be displayed in the console, but unfortunately cannot be shown on the user interface. Any efforts to map the results will result in an Un

Can anyone help me troubleshoot an issue with displaying information from a free API on my page? I'm developing a cryptocurrency app using React, and while I can see the array data with console.log(response.data), I encounter an error when I try to se ...

Animating text with a shake effect using JQuery

I am attempting to create a shake effect on my text input field when validation fails. While I have achieved the shake effect, I am unsure how to customize the default values for direction, distance, and times. Additionally, I believe there is an error i ...

Combining numerous objects into one object within an array, each with distinct keys, and displaying them using FlatList

I'm struggling to present this information in a FlatList. Array [ Object { "-N1gqvHXUi2LLGdtIumv": Object { "Message": "Aeaaeaea", "Message_CreatedAt": 1652167522975, "Message_by_Ema ...

When the settings are saved in PHP and AJAX, the password fields are automatically cleared

On my settings page, users can update their information. All fields are working correctly except for the password field. Currently, when a user clicks submit, it updates the password to nothing in the MySQL database. What I want is for nothing to happen in ...

Looking to retrieve country, state, city, and area based on inputting a pincode value using Node.js?

I'm currently working on a web project with nodeJs and ejs. I am looking for a solution that can automatically update the country, state, city, and area fields based on the input of a pin-code (zip-code). Are there any recommended packages in node js ...

unable to pre-select default option

My dynamic options select is functioning correctly, adding the selected attribute to a specific option with an id value. However, I am encountering an issue where even with an option that has the selected attribute, the first option is always displayed as ...

Is there a way to define a type once and then use it for both a function and a component in React using TypeScript?

I have a types.ts file where I define the Redux action types, a Screen.tsx file for a component that uses the actions, and an Actions.ts file where the actions are defined. I want to find a way to declare the action type only once and then use it across bo ...

Convert a function into another function when the button is clicked

Hey there, I have an array containing years in my controller: public function getNextYear() { $years = $this->getYears(); foreach ($years as $key => $value) { $y[] = $value + 1; } return $y; } I am displaying this on my ind ...

What is the best way to obtain the present date in Nuxt directly from the server?

While conducting code tests, I realized that I required the current time from the server to run the tests effectively. In JavaScript, you can easily obtain the current date on the client side using the command: new Date();. However, when working with Nuxt ...

Utilize the Tab feature effectively in Impress.Js

Currently, I have disabled the Tab key in Impress.js so that it only moves to the next slide. However, when I try to focus on links and delete this code to let it behave normally, Impress.js crashes. Has anyone found a workaround for this issue? Appreciat ...

Utilizing Ant Design Icons without an Internet Connection

In my current project, a reactJS application utilizing ant design for the UI was recently deployed to production on locked down computers. These computers lack internet access, causing ant design icons on modals to appear as empty boxes. Upon investigation ...

The Angular factory function was unable to be initialized

I recently started learning how to integrate Angularjs with ROR using this helpful tutorial While working on adding a new function to retrieve all posts from the database, I encountered a problem. The page no longer loads when I run the code and the HTML ...

Utilizing ag-grid with Vue.js: Implementing TypeScript to access parent grid methods within a renderer

I've integrated ag-grid into my project and added a custom cell renderer: https://www.ag-grid.com/javascript-grid-cell-rendering-components/#example-rendering-using-vuejs-components Although the renderer is working well, I'm facing an issue whe ...