-1

I have two text headers. I want to change the colour of the first header when the user hovers over the second one and vice versa.

#frist{
 }
 
#second{

}
<h1 id="first">First header</h1>
<h1 id="second">Second header</h1>
equalsound
  • 187
  • 1
  • 2
  • 15
  • 1
    Possible duplicate of [How to affect other elements when a div is hovered](https://stackoverflow.com/questions/4502633/how-to-affect-other-elements-when-a-div-is-hovered) – equalsound Jan 27 '18 at 22:57
  • @denmch i thought this is one part of learning process and what makes you so sure that I dont know enough .... –  Jan 27 '18 at 23:00
  • @denmch You can use more than one `

    ` tag per page. Pre HTML5, using only one h1 element helped SEO engines, these engines now have better algorithms. Misusing these tag can still result in a negative SEO impact.

    – Ricky Ruiz Jan 27 '18 at 23:38
  • @denmch What is "wrong" at this comment https://stackoverflow.com/questions/48481367/changing-property-on-hover-of-another-tag/48481500#comment83956319_48481367 ? – guest271314 Jan 27 '18 at 23:46
  • @denmch https://www.youtube.com/watch?v=WsgrSxCmMbM – Ricky Ruiz Jan 27 '18 at 23:59

6 Answers6

1

You can get this to work one way but unfortunately not the other with CSS. As seen in the JSFiddle demo below, the following code allows you to hover on the first heading and change the colour of the second one. You cannot however do this the other way round, as the item you are hovering on has to be above the item whose CSS you want to alter in the HTML document. See this SO answer for more info.

#first:hover + #second {
    color:red;
}

JSFiddle Demo 1 (No javascript)

You can however achieve this functionality with javascript, and I have included another JSFiddle Demo (linked at the bottom) to show you how you would get the result you wanted. Javascript allows you to select any element in the document with the following code:

var first = document.getElementById("first");
var second = document.getElementById("second");

You can then add an event listener to detect when that element is hovered:

first.onmouseenter = function(){

}

If the element has been hovered, you can change the CSS of any other element on the page (even if it before the hovered element):

first.onmouseenter = function(){
    second.style.color = "red";
}

//onmouseleave changes the colour back when the mouse leaves the element

first.onmouseleave = function(){
    second.style.color = "black";
}

second.onmouseenter = function(){
    first.style.color = "red";
}

//onmouseleave changes the colour back when the mouse leaves the element

second.onmouseleave = function(){
    first.style.color = "black";
}

Link to JSFIddle Demo with Javascript

JJ Gerrish
  • 812
  • 1
  • 10
  • 25
1

You could do it with JavaScript like so:

const first = document.getElementById('first');
const second = document.getElementById('second');

const toggleActive = selector => selector.classList.toggle('active');

const addHoverListeners = (trigger, target) => {
  trigger.addEventListener('mouseover', () => toggleActive(target));
  trigger.addEventListener('mouseout', () => toggleActive(target));
};

addHoverListeners(first, second);

addHoverListeners(second, first);
.active { color: dodgerblue; }
<h1 id="first">First header</h1>
<h1 id="second">Second header</h1>
Ricky Ruiz
  • 25,455
  • 6
  • 44
  • 53
0

If you want to solve with jQuery https://jsfiddle.net/gtnc6mcs/

$("#first").mouseover(function(){
    $("#second").css({
    'background-color' : '#ccc'
  });
}).mouseout(function() {
  $("#second").css({
    'background-color' : 'transparent'
  });
});

$("#second").mouseover(function(){
    $("#first").css({
    'background-color' : '#ddd'
  });
}).mouseout(function() {
  $("#first").css({
    'background-color' : 'transparent'
  });
});

or with javascript https://jsfiddle.net/gtnc6mcs/1/`

var first = document.getElementById('first');
var second = document.getElementById('second');

first.addEventListener('mouseover', function () {
  second.style.backgroundColor = '#ccc';
});
first.addEventListener('mouseleave', function () {
  second.style.backgroundColor = 'transparent';
});

second.addEventListener('mouseover', function () {
  first.style.backgroundColor = '#ddd';
});
second.addEventListener('mouseleave', function () {
  first.style.backgroundColor = 'transparent';
});
Sinisa
  • 82
  • 1
  • 4
0

With jQuery you can do that very quickly.

You can attach the events mouseenter and mouseleave.

Run the code snippet:

var $second = $('#second');
var $first = $('#first');

$second.on('mouseenter', () => {
  $first.addClass('over-first');
}).on('mouseleave', () => {
  $first.removeClass('over-first');
});

$first.on('mouseenter', () => {
  $second.addClass('over-second');
}).on('mouseleave', () => {
  $second.removeClass('over-second');
});
#first {

}

#second{

}

.over-first {
  color: orange !important;
}

.over-second {
  color: red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1 id="first">First header</h1>
<hr>
<h1 id="second">Second header</h1>

See? the header element changes its font-color.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ele
  • 33,468
  • 7
  • 37
  • 75
-1

For when hovring first element you can do it using CSS element+element Selector but for the second one you will need javascript using nextElementSibling and previousElementSibling Property see the snippet

window.onload = function() {

 document.body.addEventListener("mouseover",function(e) {
  e = e || window.event;
  var targetElem = e.target || e.srcElement;
    
  switch(targetElem.nodeName) {
   case 'H1':
    hovering(targetElem);
   break;
      default:
      var heading = document.getElementsByTagName("h1");
      for(var i = 0 ;i < heading.length;i++){
      heading[i].style.backgroundColor = '';
      }
  }
 },false);

}
function hovering(elm){
 if(elm.nextElementSibling.tagName == 'H1'){
  elm.nextElementSibling.style.backgroundColor = 'blue';
  elm.style.backgroundColor = '';   
   }
   else {
    elm.previousElementSibling.style.backgroundColor = 'blue';
    elm.style.backgroundColor = '';
  }
  
}
body {
    width: 100%;
    height: 100vh;
}
<h1 id="first">First header</h1>
<h1 id="second">Second header</h1>
<h1 id="first">First header</h1>
<h1 id="second">Second header</h1>
<h1 id="first">First header</h1>
<h1 id="second">Second header</h1>
M0ns1f
  • 2,705
  • 3
  • 15
  • 25
-1

Using CSS you can set #first and #second margin to 0; set #first, #first:hover + #second, and #second:hover to the same default value; set #second, #first:hover, body:hover h1:not(#second) to the same value, to toggle the properties at each adjacent element at :hover

#first, #second {margin:0}
#first, #first:hover + #second, #second:hover {background:red}
#second, #first:hover, body:hover h1:not(#second) {background:green}
<h1 id="first">First header</h1>
<h1 id="second">Second header</h1>
guest271314
  • 1
  • 15
  • 104
  • 177
  • Unfortunately, nothing happens when you hover just over "Second header" ("First header" does not change) – soktinpk Jan 27 '18 at 23:45