I am facing a dilemma with styling that I can't seem to resolve. There is a basic toggle feature on my page with two options -- the user can select either Toggle1
or Toggle2
, resulting in different data being displayed dynamically based on the active toggle:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="toggle1">Toggle 1</div>
<div id="toggle2">Toggle 2</div>
</body>
</html>
When a toggle is active, it should have a blue background. When a toggle is inactive, it should have a white background but turn orange when hovered over. Initially, upon page load, toggle1
is selected.
Everything works perfectly upon the initial page load -- toggle1 is selected and displayed in blue, while toggle2 is white with an orange hover effect. However, after toggling between the options, the orange hover effect seems to stop working. The reason for this issue eludes me. Could it be due to the elements being marked as "active" post-click? I cannot pinpoint the exact cause.
I have attempted implementing the hover effect using both CSS and jQuery:
CSS:
#toggle1:hover, #toggle2:hover {
background: lightblue;
}
jQuery:
$(toggle1).hover(
function () {
$(this).addClass("blue");
},
function () {
$(this).removeClass("blue");
}
);
$(toggle2).hover(
function () {
$(this).addClass("blue");
},
function () {
$(this).removeClass("blue");
}
);
While both methods successfully apply the hover effect on page load, they fail to do so consistently after a toggle has been clicked. Hover functionality ceases to work altogether.
In terms of the onClick function, it is handled in JavaScript:
JS:
const showToggle1 = () => {
// ...code to show toggle 1 content
// switch background colors from blue to white
$(toggle2).css({ background: "#FFFFFF", color: "#000000" });
$(toggle1).css({ background: "#0074d9", color: "#FFFFFF" });
}
const showToggle2 = () => {
// code to show toggle 2 content
// switch background colors from blue to white
$(toggle1).css({ background: "#FFFFFF", color: "#000000" });
$(toggle2).css({ background: "#0074d9", color: "#FFFFFF" });
}
I can only assume that there might be some interference between the onClick functions and the hover effect, causing it to malfunction after toggles have been switched. Yet, identifying the root cause remains elusive.