On hover, add active state to div, then check if div is active, then if active, add other active state to separate div

Viewed 315

I have a series of parent divs with the first of those parents having a default active state on load.
The active class is removed and added to other parent divs when they're hovered.
However, I'd also like my function to add a separate active class state to another div (technically a child div) when the parent div has its active class.
First child div also has an active state on load.
I am using jQuery and not CSS for this because I also have third div shown when hovered over its corresponding parent, hence hov1, hov2, etc.
Plus I want the default active states on load.

Here is my jQuery:

$('.parent').hover(function() {
    $('.active-parent').removeClass('active-parent');
    $(this).addClass('active-parent');
    if ($(this).hasClass('active-parent')) {
        $('.arrow-right').addClass('arrow-right-hov')
    }
    else {
        $('.arrow-right').removeClass('arrow-right-hov')
    }
}),

Here is the HTML:

<h1 class="parent active-parent hov1">
    Cybersecurity
    <div class="arrow-right arrow-right-hov"></div>
</h1>
<h1 class="parent hov2">
    High-Performance Computing
    <div class="arrow-right"></div>
</h1>
<h1 class="parent hov3">
    High-Frequency Trading
    <div class="arrow-right"></div>
</h1>

I would greatly appreciate any help - thank you!

EDIT:

This is the javascript that ended up working. No more if statements. Initial arrow-right-hov and active-parent default states are removed when hovering on another div with the same parent class. arrow-right-hov is added when the active-parent class is present. Active states remain on mouseout.

$('.parent').hover(function() {
    $('.active-parent').removeClass('active-parent');
    //remove default parent active state when hovering another parent

    $(this).addClass('active-parent');
    //add active parent state on hovered div, remain active

    $('.arrow-right-hov').removeClass('arrow-right-hov')
    //remove default secondary active child class on parent hover

    $(this).children(".arrow-right").addClass('arrow-right-hov');
    //add active child class on active parent
}),

Thank you to ikiK for getting me in the right direction!

2 Answers

Is this effect you where after?

If so, you do not need if statement;

    $(this).addClass('active-parent');
//on this line you have added the class too hovered H1
    if ($(this).hasClass('active-parent')) {
// so this right after will always be true

And you can simply target

$(this).children(".arrow-right")

to apply it to children with class .arrow-right

let me know if i got something wrong, to adjust it.

$('.parent').hover(function() {
  $('.active-parent').removeClass('active-parent');
  $('.arrow-right').removeClass('arrow-right-hov');
//first remove from all on any parent hover
    $(this).addClass('active-parent');
    $(this).children(".arrow-right").addClass('arrow-right-hov');
//then add only to hoverd

})
.active-parent {
  color: red
}

.arrow-right-hov {
  color: blue
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 class="parent active-parent hov1">
  Cybersecurity
  <div class="arrow-right arrow-right-hov">arrow-right</div>
</h1>
<h1 class="parent hov2">
  High-Performance Computing
  <div class="arrow-right">arrow-right</div>
</h1>
<h1 class="parent hov3">
  High-Frequency Trading
  <div class="arrow-right">arrow-right</div>
</h1>

Welcome to SO

Sometimes JS is not needed. CSS and the :hover pseudo can achieve the same result:

/* STYLE ACTIVE and HOVER */
.parent.active-parent,
.parent:hover {
  color: HOTPINK;
}

/* STYLE ACTIVE and HOVER arrows */
.parent.active-parent .arrow-right,
.parent:hover .arrow-right{
  color: DEEPPINK;
}

.arrow-right:after {content: "\279c";}
<h1 class="parent active-parent hov1">
  Cybersecurity
  <span class="arrow-right"></span>
</h1>
<h1 class="parent hov2">
  High-Performance Computing
  <span class="arrow-right"></span>
</h1>
<h1 class="parent hov3">
  High-Frequency Trading
  <span class="arrow-right"></span>
</h1>

If you want you could even target a parent element and override styles when tha tcommon parent has the state :hover

Related