I would like to create a JS script to highlight the spans that have a corresponding index in two different containers. I managed to create that script and it works, however, I would like to add one detail : when one of the two corresponding span is empty the other one should be highlighted in a different color.
I tried this :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<title>Textes en Parallèle</title>
<style>
body {
display: flex;
}
.text-container {
flex: 1;
margin: 10px;
border: 1px solid #ccc;
padding: 10px;
overflow-y: scroll;
max-height: 300px;
}
.highlight {
background-color: yellow;
}
.highlight-empty {
background-color: #8eff8e; /* Couleur verte pour le surlignage */
}
</style>
</head>
<body>
<div class="text-container" id="text1">
<!-- first version of the text -->
<span>span1.</span> common text
<span>span 2.</span>
<span></span>
</div>
<div class="text-container" id="text2">
<!-- second version of the text -->
<span>corresponding span 1.</span> common text
<span>corresp span 2</span>
<span>add => empty span on the other side</span>
</div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const textContainers1 = document.getElementById('text1').querySelectorAll('span');
const textContainers2 = document.getElementById('text2').querySelectorAll('span');
function addHighlight(index) {
textContainers1[index].classList.add('highlight');
textContainers2[index].classList.add('highlight');
}
function removeHighlight(index) {
textContainers1[index].classList.remove('highlight');
textContainers2[index].classList.remove('highlight');
}
textContainers1.forEach((paragraph1, index) => {
paragraph1.addEventListener('mouseover', () => {
if (textContainers2[index].innerHTML.trim() === '') {
textContainers2[index].classList.add('highlight-empty');
}
addHighlight(index);
});
paragraph1.addEventListener('mouseout', () => {
removeHighlight(index);
textContainers2[index].classList.remove('highlight-empty');
});
});
textContainers2.forEach((paragraph2, index) => {
paragraph2.addEventListener('mouseover', () => {
if (textContainers1[index].innerHTML.trim() === '') {
textContainers1[index].classList.add('highlight-empty');
}
addHighlight(index);
});
paragraph2.addEventListener('mouseout', () => {
removeHighlight(index);
textContainers1[index].classList.remove('highlight-empty');
});
});
});
</script>
</body>
</html>
I don’t understand why my add => empty span on the other side is still in yellow and not in green when I over. Do you have an idea what’s wrong with my JS conditions ?
Thank you so much in advance,
3
Answers
There’s 2 issues here. Firstly your logic only adds the
.highlight-empty
class to the emptyspan
. Therefore no difference is shown. You need to amend the logic to also add that class to thespan
which does have content, so that it has an effect.Secondly, you need to make the
.highlight-empty
class in your CSS have a higher specificity so that it overrides the.highlight
class. The simple way to do this is by adding another selector to it.Here’s a working version with these changes made:
##In this case we only need to add class and the condition to highlight different span in case of any of corresponding span is empty ##