how can the class hide be removed when hovering over each link

How can I remove the class hide from a div with the class popover when hovering over each link? I have tried implementing the code below, but it doesn't seem to be working as expected.

$(document).ready(function() {
  $(".myPopover").each(function() {
    var doc = $(this);
    doc.mouseover(function() {
      doc.find('.popover').removeClass('hide').addClass('show');
    });
  });
});
.popover {
  width: 240px;
  padding: 10px;
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.15);
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, .2);
  z-index: 1000000;
  position: relative;
  top: 10px;
  transition: 1s;
  background-color: #fff;
}

.popover.hide {
  opacity: 0;
}

.popover.show {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a href="#" class="myPopover">help</a>
<div class="popover hide">
    <h4>title</h4>
    <p>content</p>
</div>

<a href="#" class="myPopover">help2</a>
<div class="popover hide">
    <h4>title2</h4>
    <p>content2</p>
</div>

Answer №1

A non-JavaScript solution has been crafted, which proves to be beneficial in cases where JavaScript is disabled. It is advisable to resort to JavaScript only when HTML & CSS fall short in achieving the desired outcome; something as straightforward as this can easily be achieved with plain HTML & CSS.

body {
  background: white;
}

.popover {
  width: 240px;
  padding: 10px;
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.15);
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, .2);
  z-index: 1000000;
  position: relative;
  top: 10px;
  transition: 1s;
  background-color: #fff;
}

.popover {
  opacity: 0;
}

.myPopover:hover ~ .popover {
  opacity: 1 !important;
}
<div>
  <a href="#" class="myPopover">help</a>
  <div class="popover hide">
    <h4>title</h4>
    <p>
      content
    </p>
  </div>
</div>

<div>
  <a href="#" class="myPopover">help2</a>
  <div class="popover">
    <h4>title2</h4>
    <p>
      content2
    </p>
  </div>
</div>

Answer №2

To optimize the code, consider binding an event handler to all elements with the "myPopover" class simultaneously instead of looping through each one:

$(document).ready(function() {

  $(".myPopover").hover(
    function() {
      $( this ).next('.popover').removeClass('hide').addClass('show');
    }, function() {
      $( this ).next('.popover').removeClass('show').addClass('hide');
    }
  );

});

Moreover, depending on the version of jquery being used, you can utilize the built-in functions $('.popover').hide() and $('.popover').show() provided by jquery

$(document).ready(function() {
    
    
      $(".myPopover").hover(
        function() {
  $(this).next('.popover').removeClass('hide').addClass('show');
        }, function() {
 $(this).next('.popover').removeClass('show').addClass('hide');
        }
      );
    
});
.popover {
  width: 240px;
  padding: 10px;
  box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.15);
  border: 1px solid #ccc;
  border: 1px solid rgba(0, 0, 0, .2);
  z-index: 1000000;
  position: relative;
  top: 10px;
  transition: 1s;
  background-color: #fff;
}

.popover.hide {
  opacity: 0;
}

.popover.show {
  opacity: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<a href="#" class="myPopover">help</a>
<div class="popover hide">
  <h4>title</h4>
  <p>
    content
  </p>
</div>
<a href="#" class="myPopover">help2</a>
<div class="popover hide">
  <h4>title2</h4>
  <p>
    content2
  </p>
</div>

Answer №3

Try out this snippet:

doc.next('.popover').removeClass('hide').addClass('show');

When using the find method, it locates the element within it, but in this case, you have .popover positioned next to the anchor tag.

In any case, by using mouseover to remove the "hide" class and add the "show" class, your popup will only open and not close. You may want to handle this by incorporating a hover function to display and hide your popup, or include a close button to allow users to close it.

I hope this explanation proves helpful. Thank you!

Answer №4

To remove the "hide" class from a div that comes after a link when the link is hovered over, follow these steps:

$(".myPopover").hover(function(){ // Triggered when hovering over a link with the class myPopover
    var div = $(this).next(".popover"); // Selects the next sibling element if it has the class popover
    $(div).removeClass("hide").addClass("show"); // Removes hide class and adds show class to the selected div
});

Answer №5

Try a unique method by utilizing the function on :

$( window ).on('mousemove', '.customTooltip', function(e){
     $( this ).next('.tooltip').removeClass ('hidden');
});

Assigning ids to your elements can also simplify the process of locating them regardless of their position.

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

Tips for displaying AJAX response on a new webpage

I am currently working on a mobile app using Phonegap and Intel XDK. My goal is to display an AJAX response on a new HTML page. After doing some research online, I came across the solution of using window.open();. However, this method did not work for me ...

Guide on inserting an item into a vertical navigation menu

When it comes to creating a vertical menu, I rely on Bootstrap 5. https://i.sstatic.net/8CWYK.png The issue I'm facing is how to add a black background to the top of the left menu. https://i.sstatic.net/yoEcO.png I am unsure whether I should imple ...

Invoke the session on a different page and incorporate it into your JavaScript code

My php files, ajax.php and index.php, contain a mix of php code, html, and javascript. I am developing a quiz website where questions are retrieved from a database using sql in ajax.php and displayed on index.php through an ajax method. The user's sco ...

Creating JavaScript validation conditions based on certain criteria

I am currently working on validating whether a username exists in the database or not. If it does exist, an error message should be displayed, and the user should be prevented from adding details to the database until they edit the username field to make i ...

Instead of modifying the selected class, jQuery generates inline styles

Utilizing the following code to dynamically create a class: $("head").append('<style type="text/css"></style>'); var newStyleElement = $("head").children(':last'); newStyleElement.html('.move{transform: translateX(1 ...

Incorporate dynamic rules into a CSS stylesheet

I am trying to dynamically add a rule to my CSS for each element. Each rule should have a different background-image and name. However, I seem to be encountering an issue where the dynamically added div is not linking to the dynamically added CSS rule. I&a ...

Prevent the Rain from descending

Looking for a way to toggle a particle emitter on or off, I've encountered memory leaks with my Reactjs code that generates rain/snow particles using the canvas element. Despite attempts to stop the animation properly, it seems to be projecting a new ...

Accessing the .data() function is successful when referring to the element using $(this), but it does not work when using $($'#id_of_element'

I have encountered an issue while developing a jQuery plugin. I need to add data to the element inside the plugin using .data() and retrieve it later, but I am facing difficulties when trying to access the stored data by referencing the element with its id ...

Sending Django log files via AjaxORTransmitting Django log

After following a solution on Stack Overflow about reading log files in Django from /var/log/gateway, I successfully displayed the file content in the terminal. Here is an example: 2013-05-09T11:15:02.539091+08:00 localhost gateway[5205]: System starting ...

When the page loads, the HTML side menu will automatically scroll to the active item in a vertical

My website features a vertical side menu with approximately 20 items. The issue I am facing is that when a user clicks on an item, the destination loads but the side menu must be manually scrolled to find the active item if it's located at the bottom ...

jquery to create a fading effect for individual list items

I have a group of items listed, and I would like them to smoothly fade out while the next one fades in seamlessly. Below is the code I've been working on: document.ready(function(){ var list_slideshow = $("#site_slideshow_inner_text"); ...

Looking for a jQuery tool that transforms a standard table into a sortable masterpiece?

Is there a way to enhance a basic HTML table by using a jQuery plugin that allows for sorting rows based on any column? For example, something along the lines of: $('table.real_estate_values').awesomeTablePlugin(); ...

Unable to maintain constant slide down feature for jQuery dropdown

I am currently working on a website for a client and I am still learning the ropes of jQuery. The website required a dropdown menu to display links to galleries in a list format instead of having them all on the navigation bar. However, I encountered an is ...

``Is there a reason why JavaScript/jQuery events are not functioning properly within a PHP foreach

Although I have a good understanding of foreach loop in PHP, I am facing a new challenge where the script within the foreach loop only works for the first item. This is puzzling to me and I'm trying to figure out why. In my code, I am retrieving an i ...

Permanent Solution for HTML Textbox Value Modification

https://i.sstatic.net/nB58K.pngI'm currently working on a GPS project where I am attempting to capture the altitude (above sea level) value in textbox1 and convert it into a ground level value displayed in textbox2. In this scenario, the GPS Altitude ...

Unusual Actions from the Text Field

Please take a look at this link <div id="textarea_wrapper"> <textarea>How and where is my width derived from?</textarea> </div> #textarea_wrapper{ height: 250px; border:thick solid green; } textarea{ background-color: #930; ...

Separating jQuery functionalities

I've encountered an issue with my jQuery code that is supposed to highlight and modify certain links on a webpage. The problem is that some functions are affecting all the links on the site, rather than just the specific ones within a targeted div ele ...

Creating three boxes within a single div using standard HTML and CSS

I created this design using Microsoft Paint and now I want to bring it to life using HTML and CSS. https://i.stack.imgur.com/dwNZi.png The numbers represent the box numbers in the design. Here is my attempt at coding this: HTML file <!DOCTYPE html& ...

Repurposing JavaScript objects after clearing their contents

Here's my issue. I'm working with a Javascript object, initialized as var stack = {}. This object is used in my project to store arrays. When the user clicks the add button, an array is added to the object with a specific key that is inputted in ...

Storing css data for iPhone Facebook application

Currently, I am in the process of developing a webpage specifically for the iPhone Facebook app wall link. Interestingly, when attempting to apply the margin attribute to a div within the webpage, the margin doesn't seem to have any effect when clicki ...