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!