Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:06:22+00:00 2026-05-31T23:06:22+00:00

I have the following html structure <div> text 1 <div> text 2 <div> text

  • 0

I have the following html structure

<div>
    text 1
    <div>
        text 2
        <div>
            text 3
            <div>
                text 4
            </div>
        </div>
    </div>
</div>​

with the following jquery

$('div').hover(
    function() {
        $(this).addClass("hover");
    },
    function() {
        $(this).removeClass("hover");
    }
);​

jsFiddle

When I hover to the last div with text 4, all the above div elements will get the class hover.

Is there a clever way to only put the hover class on the last hovered div element? So when I hover to text 3 only the surrounding div wil get the hover class.

I don’t want to wrap the text in a new element since the applied situation is a little more complex than this. I also can not use CSS3.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:06:23+00:00Added an answer on May 31, 2026 at 11:06 pm

    On mouse over, add the class to the current element, then remove the class from all its ancestor divs using .parents():

        function() {
            $(this).addClass("hover").parents('div').removeClass("hover");
        },
    

    On mouse out, remove the class from the current element and add the class to its parent div using .parent(), if its parent is one. This way, when the mouse moves from the current div to its parent div, it’ll receive the hover effect:

        function() {
            $(this).removeClass("hover").parent('div').addClass("hover");
        }
    

    The selector that you use in .parents() and in .parent() must be the same selector that you use with $() so you don’t affect the wrong elements.

    jsFiddle preview

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML code / structure: <div id=container> <div id=div1>text</div> <div id=div2>more
I have the following html structure <div id=content> <div id=transport> <div id=header>Header Text</div> <div
I have the following html structure: <div class=decorator> <div class=EC_MyICHP_Item> <div class=text> <h3><a target=_blank
I have the following HTML structure: $('#subgroup a').nextUntil('h3').wrapAll('<div></div>'); <script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js></script> <div id=subgroup> <h3>Group name
I have the following HTML node structure: <div id=foo> <div id=bar></div> <div id=baz> <div
i have following HTML structure: <div class=container> <div class=sidebar></div> <div class=content></div> <div class=subscript></div> </div>
onI have the following html structure: <div> <ul> <li class=gravity-form></li> <li class=gravity-form-divider></li> </ul> </div>
I have the following HTML structure <div id=test-1-yay></div> ... bunch of code ... <div
I have the following html structure <div style=overflow-x:auto;overflow-y:hidden;position:relative> <div style=position:absolute; top:0;bottom:0;padding-top:30px> </div> </div> When
I have the following html structure: <div class=container> <div class=header></div> <div class=sidebar></div> <div class=content-loading></div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.