Is there a way to display different descriptions or images when hovering over each word in a paragraph using HTML? It seems that only the last element is being retrieved and displayed across the entire paragraph. Any suggestions on how to fix this issue would be greatly appreciated!
For simplicity, I've included just the code related to displaying text descriptions below, omitting any non-essential parts. If anything is unclear, please feel free to ask for clarification. Thank you for your help.
JSON data:
var json = {
"parameter1": {'Anyway': 0.822, 'if': 0.91, 'you': 0.53, "don't": 0.9, 'agree': 0.53},
"parameter2": {'Anyway': 0.1322, 'if': 0.4001, 'you': 0.523, "don't": 0.509, 'agree': 0.201},
"parameter3": {'Anyway': 0.822, 'if': 0.101, 'you': 0.423, "don't": 0.225, 'agree': 0.61},
"parameter4": {'Anyway': 0.72, 'if': 0.201, 'you': 0.603, "don't": 0.869, 'agree': 0.99},
"parameter3_text": {'Anyway': "description1", 'if': "description2", 'you': "description3", "don't": "description4", 'agree': "description5"}
}
Part of a switch statement (other parameters omitted):
case "parameter3":
for(var x = 0; x < words.length; x++) {
var span = "<span class='tooltiptext'>" + json.parameter3_text[Object.keys(json.parameter3_text)[x]] + "</span>"
spans.push(span);
}
// setting colored spans as paragraph HTML
paragraph.innerHTML = spans.join(" ");
break;
HTML code
<div class="parameter3">
<p id=p3><span>Anyway if you don't agree</span></p>
</div>
CSS code
.parameter3 {
position: relative;
}
.parameter3 .tooltiptext {
visibility: hidden;
width: 120px;
background-color: white;
color: #000;
text-align: center;
border-radius: 5px;
padding: 5px 0;
border: solid;
/* Position the tooltip */
position: absolute;
z-index: 1;
}
.parameter3:hover .tooltiptext {
visibility: visible;
top: 30px;
left: 20px;
}