How do add a class to another div, when hovering over one div?

Waleed Afridi

I am trying to add class to .sectionmenu div, but for some reason it adds the class to both the div elements when I hover over the .tabone .toggleClass, the div has same the class.

When you hover our first div i.e .tabone .toggleClass it should add class to first div .sectionmenu, similarly when you hover our the second div i.e .tabone .toggleClass it should add class to second div .sectionmenu.

(function($) {
  $(document).ready(function() {
    $('.tabone .toggleClass').hover(function() {
      var mine = $(this).closest('.menubox');
      $(this).closest('.main-section').find('.sectionmenu').not(mine).removeClass('class_name');
      mine.addClass('class_name');
    });
  });
}(jQuery));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-section">
  <div id="col1">
    <div class="hidden">hidden text</div>
  </div>
  <div id="col2" class="class">
    <div class="menubox tabone"> --> when hover over this div it should add class to first .sectionmenu div
      <a class="toggleClass">toggleClass</a>
      <a class="toggleClass">toggleClass</a>
    </div>
    <div class="menubox tabone">
      <a class="toggleClass">toggleClass</a>
      <a class="toggleClass">toggleClass</a>
    </div>
  </div>
  <div id="col3" class="class">
    <div class="sectionmenu">
      <div class="menubox">
        <a class="toggleClass">toggleClass</a>
        <a class="toggleClass">toggleClass</a>
      </div>
    </div>
    <div class="sectionmenu">
      <div class="menubox">
        <a class="toggleClass">toggleClass</a>
        <a class="toggleClass">toggleClass</a>
      </div>
    </div>
  </div>
</div>

David Thomas

One approach is as follows, here we use jQuery to add a custom data-* attribute in order to relate the elements together:

// here we select elements with an 'id' attribute that starts with
// the string 'col',
// we then iterate over that collection of elements using the
// each() method:
$('[id^=col]').each(function() {
  // caching variables to avoid - where possible - repeated look-ups
  // for the same items;
  // here we cache the current element:
  let ancestor = $(this),
    // we then cache the '.menubox' descendants of the
    // current element:
    menuboxes = ancestor.find('.menubox'),
    // we then cache the '.toggleClass' elements:
    toggleClassLinks = ancestor.find('.toggleClass');

  // iterating over the menuboxes collection, and setting the
  // custom 'data-*' attribute (here named 'data-index'),
  // to contain the index of the current .menubox element
  // within the collection, or its index within the current
  // [id^=col] element:
  menuboxes.attr('data-index', function(i) {
    return i;
  });

  // if there are a non-zero number of '.toggleClass' elements:
  if (toggleClassLinks.length) {
    // we iterate over that collection and use the on() method
    // to bind the anonymous function as an event-handler for
    // the 'mouseenter' event:
    toggleClassLinks.on('mouseenter', function(e) {
      // caching the current .toggleClass element:
      let target = $(this),
        // caching the various elements:
        grandparent = target.closest('.main-section'),
        parent = target.closest('.menubox'),
        // caching the attribute-value of the 'data-index'
        // attribute we set earlier, using the data() method
        // (because of a peculiarity of the method we couldn't
        // set the attribute using that method, but retrieving
        // is consistent):
        index = parent.data('index');

      // here we find the elements with the class of 'class_name' within the
      // ancestor element, and remove that class:
      grandparent.find('.class_name').removeClass('class_name');

      // here we use a template-literal string to interpolate the 'index'
      // variable into the string, to create an attribute-selector wherein
      // the index is equal to the index of the currently hovered .toggleClass
      // element's parent:
      grandparent.find(`[data-index=${index}]`)
        // we then filter out the current .toggleClass element's parent:
        .not(parent)
        // and add the class_name class to the other remaining element(s)
        // that matched the original selector:
        .addClass('class_name');
    });
  }
});
*,
::before,
::after {
  box-sizing: border-box;
  font: 1rem / 1.5 sans-serif;
  font-weight: normal;
  margin: 0;
  padding: 0;
}

div {
  border: 1px solid #000;
  padding: 0.5em;
}

.main-section {
  border-color: transparent;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1em;
  min-height: 50vh;
}

.main-section>div {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.class_name,
.class_name .toggleClass {
  color: #f90;
  transition: color 0.3s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="main-section">
  <div id="col1">
    <div class="hidden">hidden text</div>
  </div>
  <div id="col2" class="class">
    <div class="menubox tabone">
      <a href="#" class="toggleClass">toggleClass</a>
    </div>
    <div class="menubox tabone">
      <a href="#" class="toggleClass">toggleClass</a>
    </div>
  </div>
  <div id="col3" class="class">
    <div class="sectionmenu">
      <div class="menubox">
        <a href="#" class="toggleClass">toggleClass</a>
      </div>
    </div>
    <div class="sectionmenu">
      <div class="menubox">
        <a href="#" class="toggleClass">toggleClass</a>
      </div>
    </div>
  </div>
</div>

JS Fiddle demo.

References:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to display a div when hovering over another div

Changing the style of one div when hovering over another

How to scroll up a div when hovering over another element

How to change css of one class when hovering over another?

jQuery hovering over one div to trigger changes in another div

Add class to one div when another is in the viewport

Change the Brightness of a div when hovering over another div

Change class of other div when hovering on one div

How to add class to another div, When a div has toggle class?

How to overlay one div over another div

How to make an element active on hover, and remain active when im hovering over another specific div

How to change class of separate div when hovering over link using JQuery

change css of one div by hovering on another div

How do I change the class of several tds when hovering over one of them?

How to show one div over another one?

Hovering over image and displaying text in another div

Hovering over 1 div to change content in another

How to trigger div visibility when hovering over list item in CSS?

how to hide one div and hide another on hovering image or text?

Displaying button when hovering over div in TailwindCSS

Changing divs when hovering over a different div

Hovering one item to display the child in another div

Add class to div when another empty

Affect another div when hovering CSS

How to overlay one div over another div (no position absolute ... )?

how to draggable over one div to another div using jquery?

How to overlap one div over another div without position

Add divs on top of an div, when hovering

How to have a div show up when hovering over elements but not appear over the mouse pointer?