Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
105
rated 0 times [  112] [ 7]  / answers: 1 / hits: 107705  / 12 Years ago, sun, february 10, 2013, 12:00:00

I have seen this jQuery syntax:



if($(element).is(':hover')) { do something}


Since I am not using jQuery, I am looking for the best way to do this in pure javascript.



I know I could keep a global variable and set/unset it using mouseover and mouseout, but I'm wondering if there is some way to inspect the element's native properties via the DOM instead? Maybe something like this:



if(element.style.className.hovered === true) {do something}


Also, it must be cross browser compatible.


More From » jquery

 Answers
3

Simply using element.matches(':hover') seems to work well for me, you can use a comprehensive polyfill for older browsers too: https://developer.mozilla.org/en-US/docs/Web/API/Element/matches


[#80320] Friday, February 8, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
parker

Total Points: 259
Total Questions: 109
Total Answers: 97

Location: Zambia
Member since Thu, Jun 25, 2020
4 Years ago
;