Double firing of hover event when child element is hovered

I'm having trouble with creating a simple multi-level drop-down box and I can't seem to get the hover event to trigger only once when hovering over an li element. The hover event triggers twice when hovering over the a tag inside the list item.

Check out this jsFiddle that illustrates the issue. I've included extra padding around the a tag to demonstrate how the event is triggered twice. If you hover over just the padding, the event fires once, but as soon as you hover over the a tag, it fires again.

Here is the HTML code:

<ul class="dropit">
    <li><a href="#">Thing 1</a></li>
    <li><a href="#">Thing 2</a></li>
    <li><a href="#">Thing 3</a>
      <ul>
        <li><a href="#">Sub-thing 1</a></li>
        <li><a href="#">Sub-thing 2</a>
           <ul class='sub-menu'>
            <li><a href="#">Sub-sub-thing 1</a></li>
            <li><a href="#">Sub-sub-thing 2</a></li>
          </ul>
        </li>
      </ul>
    </li>
</ul>

And here's the jQuery code:

$(document).ready(function(e) {
    $('ul.dropit li').on('mouseover', function(event) {
        $target = $(event.currentTarget);
        $sub = $target.children('ul').first();
        $sub.slideToggle();
    }).on('mouseout', function(event) {
        $target = $(event.currentTarget);
        $target.children('ul').first().slideToggle();
    });
});

You can see the problem in action on this jsFiddle demonstration.

Answer №1

Try using mouseenter and mouseleave instead of mouseover and mouseout

Check out this FIDDLE for reference

$(document).ready(function(e) {
    $('ul.dropit li').on('mouseenter', function(event) {
        $target = $(event.currentTarget);
        $sub = $target.children('ul').first();
        $sub.slideToggle();
    }).on('mouseleave', function(event) {
        $target = $(event.currentTarget);
        $target.children('ul').first().slideToggle();
    });
});​

mouseover triggers when hovering over a child element.

mouseenter only fires once no matter how many children are hovered over.

Answer №2

Check out this demo on jsFiddle

To create hover effects, simply use .hover()

For a smoother experience and to prevent multiple slides from stacking up during repeated hover actions, add the following line of code: .stop()

$('.dropit li:has("ul")').hover(function() {
    $('>ul', this).stop().slideToggle();
});

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

Is there a way for me to create a params hash key with a value that is equal to another hash using ajax, without

Exploring the $.ajax(); function, I am experimenting with creating a specific param key for my rails controller to handle. The parameter that I want to build should have this structure: { stack_entry: { project_name: 'project_name } } I have been t ...

Problem with responsive design on iPhone

I'm currently working on developing a responsive chatbot using CSS Bootstrap. However, I've encountered an issue where the header and footer are not fixed when I open the app on an iPhone. The keyboard header is also moving up the screen, which s ...

Is it important that the end date does not exceed the start date?

In my request, the end date cannot be later than the start date. For example, if the start date is 24/4/2017 and the end date is 23/4/2017, the search function should be disabled for the calendar date 23/4/2017. Only dates from 24/4/2017 onwards should be ...

What are some strategies for customizing the appearance of child components within a parent component?

I have a scenario where I am using parent and child components. When I use the HTML in another component, I also apply my CSS. For example, in my parent component: HTML <div class="chips"> <p class="tags">Tag 1</p&g ...

"The login page failed to redirect to a different page due to issues with the header function

Hello PHP gurus, I need some assistance from all of you. I'm facing an issue where my Log-in button is not directing to Home (Student_Home.php) even though the Student ID and password are already registered in the database. *Below is the code snippe ...

24-hour countdown tool featuring a visual progress bar

Currently, I am in the process of developing an application aimed at assisting individuals in either forming new habits or breaking old ones. In this application, I am looking to implement a countdown timer that ticks down daily; with the help of this help ...

Unable to retrieve elements following jQuerymobile page alteration

To demonstrate the issue I'm facing, I've set up a basic example on my GitHub here: https://github.com/kanesee/jqm-page-state In this scenario, I have a page called page1.html where there is a div with an id of "content" whose color is changed t ...

Is there a way to dynamically assign a class to form groups that contain blank inputs?

With a simple click on the "make pink" button, I aim to emphasize the input fields that are empty: $("#highlight").click(function () { $('.form-group input').blur(function() { if (!$(this).val()) { $(this).parents('.form-group&a ...

Successfully submitting form_with without experiencing any errors for mandatory fields

I am currently dealing with a form that triggers a sidekiq worker upon submission. The issue I am facing is that even if the form is empty, clicking submit still attempts to run the worker. What I would like is for an alert to notify the user that the fiel ...

Combining Razor Pages with Bootstrap for seamless integration, ensuring links are displayed without any unnecessary

I am puzzled as to why a margin or space between my action links disappears after adding the if statement. I am confident that the HTML appears identical with or without @if (User.IsInRole("Admin")) when viewed from the admin perspective. My code is utiliz ...

Populate the table with JSON object information

I've structured a table as follows: <div class="row"> <div class="row"> <table class="table table-bordered table-striped tree-grid"> <thead class="text-primary"> <tr> <th> ...

Dealing with problems in parsing JSON data returned from a jQuery AJAX

I am currently working with the GitHub API V3 and using the following code for making an AJAX call: $.ajax({ type:'POST', url: 'https://api.github.com/gists', data: JSON.stringify({ "public": true, "files": ...

Scrolling up and down without visible scrollbars

I am facing a dilemma with a client who desires a similar functionality to the cargocollective theme nonfeed, but we have come to realize that this layout is quite challenging to navigate without specific tools like a scroll wheel, multi-touch scrolling, o ...

Use Chrome developer tools to simulate mobile devices with navigation bars

Hello there! I am a web developer seeking a method to simulate mobile devices while displaying their respective navigation bars, toolbars, and more. In Google Chrome's 'Device toolbar' (v58 on macOS), there is a specific mode available for t ...

Design elements using CSS within the <th> tags

When working with HTML tables, the use of a <th> element allows for the implementation of a scope attribute. This attribute serves to indicate whether the header cell is associated with its subsequent column, row, or group. Is it feasible to leverage ...

Issues with Implementing Scroll Directive in Angular JS

Apologies for asking what may seem like a silly question. I'm still new to using AngularJS and recently came across a neat little scroll directive on http://jsfiddle.net/88TzF/622/. However, when I tried implementing the code in the HTML snippet below ...

I am interested in modifying the color of the tick marks on the input[range] element

Seeking Slider Customization Help I am eager to learn how to create and personalize a slider using HTML. Although many resources offer guidance on customizing the slider itself, I have yet to come across any information on customizing the scale. For insta ...

Can you explain the concept of Cross-origin requests?

My JavaScript application is designed to detect single, double right, and double left clicks. A single click triggers an asynchronous request to the HTTP server, while the rest are intended to change the user interface on the client side. However, I am str ...

What is the best way to ensure a jQuery UI slider recognizes when the mouse is released after being clicked?

I have integrated the jQuery slider into my application and am facing an issue with triggering an event to update the database upon releasing the slider. Currently, I am using the following code snippet: $('.ui-slider-handle').mouseup(function () ...

Toggle the visibility of multiple DIVs within a table row, with fading effects, while also displaying various hyperlinks in another row and columns

Hello there, I have a specific requirement that I need help with... In my layout, I have 3 links placed in 3 columns within a single row. Underneath, there is another row containing 1 column with 3 div elements inside it. My question is: How can I achieve ...