21

When doing $(...your selector here...).is(":hover"), jQuery prior to 1.9.1 gave correct answer, while jQuery 1.9.1 tells you this:

Error: Syntax error, unrecognized expression: unsupported pseudo: hover

This is not about performing an action on hover - for that, just use .hover() This is about, at an arbitrary point in time, finding out whether or not some element is being hovered

Thank you Mooseman for the answer, which I shall demonstrate with a fiddle

4
  • 2
    Are you trying to answer your own question ? If so do it properly : write an answer. Commented May 11, 2013 at 13:14
  • 1
    You are making things complicated. Simply .hover() would do: jsfiddle.net/BxL4w/5
    – Antony
    Commented May 11, 2013 at 13:21
  • 2
    @Anthony: wrong. I want a boolean, is it hovered or not. I do not want to perform an action on hover. Commented May 11, 2013 at 13:54
  • You should not put the answer into the question. If you wish, you are allowed to answer your own question instead.
    – kapa
    Commented May 11, 2013 at 13:55

3 Answers 3

26

Assuming your selector is #myid, use $('#myid:hover') instead of using .is().

If you are using $(this) or a variable, use myVar.filter(':hover').

6
  • 7
    how about when using $(this) or a variable object?
    – t q
    Commented Feb 1, 2014 at 1:36
  • then, use var temp = $(yourElement).parent().find(":hover"); return temp.length == 1 && temp[0] == yourElement; Commented Mar 14, 2015 at 3:58
  • 1
    Doesn't work in jQuery 3.1.1... I try $('#myid:hover').length > 0 in an event handler.
    – Legends
    Commented Mar 1, 2017 at 16:15
  • Not always possible when you want to check elements with global classes and no ids. $(this) not possible to ":hover" in selector!
    – Kareem
    Commented Mar 12, 2017 at 6:00
  • 2
    .filter(:hover) seems to always return an object, and filter(:hover).length works. Please consider updating.
    – konsolebox
    Commented Jun 27, 2018 at 12:20
0

However the workaround above is unsuitable when you are comparing elements using jQuery.fn.is() with a composed selector which is not known beforehand, and that could match several elements in the parent container.

eg, the same exception is thrown when selectorText in style_get() below is equal to: ".mCSB_scrollTools_vertical.mCSB_scrollTools_onDrag_expand .mCSB_dragger.mCSB_dragger_onDrag_expanded .mCSB_dragger_bar, .mCSB_scrollTools_vertical.mCSB_scrollTools_onDrag_expand .mCSB_draggerContainer:hover .mCSB_dragger .mCSB_dragger_bar"

..because hover is not defined in Sizzle.selectors.pseudos or Sizzle.selectors.setFilters (jquery-2.1.4, line 1764)..

function style_get(elem) {

  var sheets=document.styleSheets;
  var css_properties={};
  elem=$(elem);

  for (var s in sheets) {
    var rules=sheets[s].rules || sheets[s].cssRules;

    for (var r in rules) {
      if (elem.is(rules[r].selectorText)) {

        css_properties=$.extend(
          css_properties,
          css.parse(rules[r].style),
          css.parse(elem.attr('style'))
        );

      }
    }

  }

  return css_properties;

}
2
  • Which "workaround" are your referring to that is "unsuitable"? Are you commenting on another post? If so you should post a comment on that post instead. Your answer seems incomplete as is.
    – Kmeixner
    Commented Jun 1, 2015 at 15:04
  • @Kmeixner, The OP wrote "Here is a workaround..." it could be the answer to what you are asking. I easily found it using ctrl+f and typing "workaround"
    – Zectbumo
    Commented Nov 30, 2017 at 1:36
0

You could try to use .filter()

I think, my solution will help you:

//
// jQuery 3 pseudo ':hover' doesn't support
//

// Working on jQuery 1.7.1
$("*").on('click', $.proxy(function() {
    if ( this.$('.tooltip:hover').length > 0 ) { // jQuery 3.2.1 -> Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: hover
      console.log('Hovered!');
    } else { 
      console.log('Where is my element!?'); 
    }
}, this));


// Updated solution for jQuery 1.7.1 - 3.2.1
$("*").on('click', $.proxy(function() {
    var isHovered = $('.tooltip').filter(function() {
        return $(this).is(":hover");
    });

    if ( isHovered.length > 0 ) {
      console.log('Hovered!');
    } else { 
      console.log('Where is my element!?'); 
    }
}, this));

also, you can see my GitHub gist:https://gist.github.com/antydemant/74b00e27790e65f4555a29b73eea7bb2

Not the answer you're looking for? Browse other questions tagged or ask your own question.