0

I've searched far and wide, and cannot find a solution.

I have two divs. One to the right of the other. In each div, I have list elements, but they are in no way parents or siblings, so I cannot use CSS.

Does anyone have a solution, where I can have a link anywhere in my html document, that when hovered, changes the class of another element, regardless of where in the html document it is placed? Javascript for example.

The idea is that when I hover an element in the left list, it will highlight the element in the right list that is considered related to it.

4
  • could you paste your html here ? Commented Jan 12, 2017 at 16:50
  • hover is jquery though right Commented Jan 12, 2017 at 16:51
  • and jQuery is JavaScript Commented Jan 12, 2017 at 16:52
  • Please read How to Ask. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". Commented Jan 12, 2017 at 16:53

2 Answers 2

1

There are many ways to achieve this. I've created an example that uses jQuery to manipulate the class on another element for the mouseenter mouseleave events (basically hover).

<div class="red">
  x
</div>
<div class="green">
  y
</div>

$('.red').on('mouseenter mouseleave', function() {
    if ($('.green').hasClass('hover')) {
    $('.green').removeClass('hover');
  } else {
    $('.green').addClass('hover');
  }
});

Sample

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! I added <script src="js/jquery-3.1.1.js" type="text/javascript"></script> <script src="js/js.js" type="text/javascript"></script> to my head (two files in a folder called "js" and the path is correct), and then the css in the CSS and the html in my html document. This is offline in a folder on my computer. The divs and the styling works, but I get no reaction from the hover. What am I missing here? The "js.js" is where I put the js, as prompted by W3 schools.
Open your browser console and see if it outputs an error
js.js:1 Uncaught TypeError: $(...).on is not a function(…)(anonymous function) @ js.js:1
Sorry, that was when I switched to <script type='text/javascript' src='ajax.googleapis.com/ajax/libs/jquery/1.6.4/…> instead of the local file. With the local library, I get no error, but also no function.
Got it to work. Was missing //<![CDATA[ window.onload=function(){ to begin with in the JQuery and }//]]> to end it with.
1

Jquery is a fairly large library. I highly suggest you do this in pure javascript if you aren't using Jquery anywhere else in your webpage. Here is an example I created that changes the class of one div when another is hovered with a simple If statement.

https://jsfiddle.net/c16rvt0y/1/

function highlightDiv(){
    if (document.getElementById("highlightMe").className === "otherDiv"){
       document.getElementById("highlightMe").className = "otherDiv otherDivHighlighted"
  } else {
     document.getElementById("highlightMe").className = "otherDiv"
  }
}

Here is an example that targets separate divs depanding on which one is called by the function:

https://jsfiddle.net/c16rvt0y/2/

function highlightDiv(highlightMeID){
    if (document.getElementById(highlightMeID).className === "otherDiv"){
       document.getElementById(highlightMeID).className = "otherDiv otherDivHighlighted"
  } else {
     document.getElementById(highlightMeID).className = "otherDiv"
  }

}

4 Comments

This can work, but it will overwrite the classes for the targeted element
You can just add a separate class onto that element that changes the border / background like I did in the second example. It doesn't overwrite it. Call the non hightlighted div with .class, and the highlighted div with .class.hightlightedClass. The second is more specific so it will override any styling that is the same in normal .class (I hope I'm making sense)
Perfectly clear, but you always have to maintain the otherDiv class in code, or whatever classes the element had before. jQuery handles that properly with add and remove class. It can be easily done with JavaScript but jQuery is easier to understand.
Large? The file was 260kb. I'm also only going to use this offline. Thank you anyways, I may try this if I cannot get the jQuery to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.