0

I have a select element, and I want to detect which option the user is hovering over. I am using the code here that ShankarSangoli has wrote but it is not working. I have tested in Chrome.

JSFiddle

$("select").hover(function (e)
{
     var $target = $(e.target); 
     if($target.is('option')){
         alert($target.attr("id"));//Will alert id if it has id attribute
         alert($target.text());//Will alert the text of the option
         alert($target.val());//Will alert the value of the option
     }
});
Community
  • 1
  • 1
Jon
  • 8,205
  • 25
  • 87
  • 146

4 Answers4

2

IE and Chrome do not support hover on option elements as limelights has pointed out. However, if you are already comfortable using jQuery you could try using the combobox it can use an underlying select but builds the options using ul and li elements.

Community
  • 1
  • 1
purgatory101
  • 6,494
  • 1
  • 20
  • 21
  • Forgot about that completely! Nice one! :D – Henrik Andersson Dec 05 '12 at 22:40
  • That is what I have always used whenever I have wanted better styling, more control or a searchable (autocomplete-style) list. There are a few quirks with doing the combobox, but nothing that is a deal-breaker...and it is nice since you can use it right on top of a native select. – purgatory101 Dec 05 '12 at 22:43
2

The functionality you're trying to achieve is not availiable in Chrome nor IE yet. This is pointed out in the question you're currently refering to.

As @purgatory 101 pointed out, a clever use of the combobox will allow you to "hook in" additional jquery which might allow you to alert(); on hover.

Just working of the source to combobox this would be a pointer in the right direction:

$('ul.ui-autocomplete ui-menu li').hover(function(){
    //do your stuff here.
});
Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
1

Your only way to accomplish this would be use use a SELECT replacement that uses HTML + JS + CSS instead of the native control.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

option:hover works in IE11 - woot!

A2D
  • 308
  • 2
  • 7