I want to create a quiz example using Jquery.However I am a bit lost with how to get the css class with hover on Jquery. My css code is the following:
.right{
background-color: white;
}
.wrong {
background-color: white;
}
.right:hover {
background-color: yellow;
}.wrong:hover {
background-color: yellow;
}
Using Jquery I change the background colors of classes wrong and right to green and red and after 1.5 seconds I return them back to normal with the following code:
//change colors
$('.right').click( function() {
var $el = $(".wrong"),
originalColor = $el.css("background-color");
var $el2 = $(".wrong:hover"),
hover = $el2.css("background-color:hover");
$('.wrong').css("background-color" , "red");
$('.right').css("background-color" , "green");
test(originalColor,hover);
})
async function test(originalColor,hover){
await sleep(1500);
$('.wrong').css("background-color" , originalColor);
$('.right').css("background-color" , originalColor);
$('.wrong:hover').css("background-color" , hover);
$('.right:hover').css("background-color" , hover);
}
function sleep(ms){
return new Promise(resolve => setTimeout(resolve, ms));
}
//change colors
Everything works fine except the hover part.After the first Jquery click function the background-color of the hover is lost.It looks like that $el2 = $(".wrong:hover") is syntactically wrong.Can you help my on how to chage the code so I do not lose the background-color: yellow; on the hover after the first Jquery function??
Fiddle example:
Fiddle example