I have 2 lists of items. One is an ordinary <ul>
and the other is a grid of images wrapped in <div>
s.
I am looking to highlight the item hovered on both lists that will work in both directions.
So if I hover over <li>Apple
, both the <li>Apple
& <div>Apple
get highlighted. And this should also work in the other direction, if I hover over the <div>Apple
, both the <div>Apple
and <li>Apple
is highlighted.
Notes:
- I am able to add the unique class name to any element. Either the
<li>
&<div>
or the<a>
within. - Highlighting can be either as an
.active
class or inline styling.
Similar to the below question but works in both directions: Jquery 'Highlight' element with the same class
<ul>
<li class="app-hover-select">
<a class="item-1" href="">Apples</a>
</li>
<li class="app-hover-select">
<a class="item-2" href="">Pears</a>
</li>
<li class="app-hover-select">
<a class="item-3" href="">Bananas</a>
</li>
<li class="app-hover-select">
<a class="item-4" href="">Pineapples</a>
</li>
</ul>
<div class="wrapper">
<div class="app-hover-select">
<a class="item-1" href="">
<img scr="">
Apples
</a>
</div>
<div class="app-hover-select">
<a class="item-2" href="">
<img scr="">
Pears
</a>
</div>
<div class="app-hover-select">
<a class="item-3" href="">
<img scr="">
Bananas
</a>
</div>
<div class="app-hover-select">
<a class="item-4" href="">
<img scr="">
Pineapples
</a>
</div>
</div>
My current jQuery with current HTML:
jQuery('.app-hover-select > a').hover(function() {
var appClass = jQuery(this).attr("class");
jQuery(appClass).toggleClass('active');
});
My logic is:
- On hover of
.app-hover-select > a
var appClass
= Get the class- Add class
active
to all elements with the classappClass