I’m using jQuery 1.12. I have this class for when someone hovers over an LI element
.select-options li:hover {
color: gray;
background: #fff;
}
How do I use jQuery to trigger applying this class to an LI element? I have tried this
$(".select").bind('keydown', function(event) {
elt = $(this).find('.select-options li:hover')
if (elt.length == 0) {
elt = $(this).find('.select-options li:first')
} // if
var next;
switch(event.keyCode){
// case up
case 38:
break;
case 40:
next = $(elt).next();
console.log($(next).text());
$(next).trigger('mouseenter');
break;
}
});
but the $(next).trigger('mouseenter'); doesn’t seem to be working. You can check out my Fiddle here — http://jsfiddle.net/cwzjL2uw/15/ . Click the “select State” drop down and then click the down key on your keyboard to trigger the block of code above.
Per MDN:
So it is not necessarily a class applied to the element, also :hover is used in the context of a pointing device interaction (for example a mouse).
In your case this should give you an starting point:
Bind your li elements to the hover event in jquery:
});
Need also to adjust your css
This is a working fiddle.
http://jsfiddle.net/sge8g5qu/