Regarding the bug:
It may be something from the Jquery library itself, something from the browser, or something in your code that caused the error, there are many possibilities and to find the solution of this code you will only be able to debug it.
Regarding the solutions:
SOLUTION I:
I advise to do this effect using CSS, besides being the function of it to do this type of interaction, you avoid doing two manipulations directly in the DOM with Jquery, saving memory.
In this case you will use a selector CSS3 that assigns interaction to the element immediately proceeding to it represented by the sum signal (+).
Example:
/*Style default*/
img {
border: 1px solid black;
height: 100px;
width: 100px;
}
/* Seletor e exemplo de interação */
#work1:hover + a + #work2 {
border-color: red;
}
<img id="work1" src="/images/summer.jpg" alt="Imagem I"><a href="#um"></a>
<img id="work2" src="/images/winter.jpg" alt="Imagem II"><a href="#um"></a>
Explanation:
I copied your HTML and with CSS assigns the event from Hover at the #work1 then I passed the path until I reached the #work2, adding the selector + between each element.
SOLUTION II:
If you choose to do so using derivatives from javascript, I suggest you use the following code:
document.querySelector('#work1').addEventListener('mouseover', function(){
document.querySelector('#work2').classList.add('tone');
});
document.querySelector('#work1').addEventListener('mouseout', function(){
document.querySelector('#work2').classList.remove('tone');
});
img {
border: 1px solid black;
height: 100px;
width: 100px;
}
img.tone {
border-color: red;
}
<img id="work1" src="/images/summer.jpg" alt="Imagem I"><a href="#um"></a>
<img id="work2" src="/images/winter.jpg" alt="Imagem II"><a href="#um"></a>
Explanation:
I select the elements using the querySelector, where the former hosts the mouseover and the second to classList, in this example it serves as the native equivalent of toggleClass but the two have a small difference.
Remarks:
- If you choose to do it with CSS, try using the selector for directly followed elements, i.e., removing the element
<a>
between them to avoid autos levels of specificity of selectors.
- For the element
<img>
remember to add value to attribute alt the same is important for SEO and accessibility and at the end you can remove the bar (/) before closing the tag, if you are using HTML5.
- In the case of javascript, you can change directly
the value of src, without having to use the "tone" class, replacing
the classList for .src="image path". Example:
document.querySelector('#work2').src="/images/click.svg";
Before a doubt: why use javascript to do Hover, if you can do this using css?
– Ivan Ferrer
I couldn’t find a solution where I did Hover in image 1 (#work1) and change image 2 (#work2) in CSS. I’m open to suggestions. Thank you for the reply.
– costinha
@Ivanferrer, could have used the image as the background of a div instead of an img tag
– Tobias Mesquita
Hello @Tobymosque, there are several ways to do, the best would surely be using a background, however, I am presenting a simple solution, another example to solve would be doing this:
.attr('style','background:url(/images/click.svg)')
... but the important thing is to work, right.– Ivan Ferrer
@Ivanferrer, I added an answer with an example using background-image.
– Tobias Mesquita