Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulate Hover in jQuery

Currently, I have some jQuery/Javascript code that toggles the css class 'ui-state-hovered' when a user hovers the mouse over certain elements, and I want to write a test in konacha to test this piece of code.

How would I write this function in Javascript with the help of jQuery?

Return true if when a user hovers over an element $('.someClass li:first'), the class 'ui-state-hovered' exists. Else return false.

How would I simulate a user hovering their mouse over that element?

like image 657
yz10 Avatar asked Jun 14 '12 17:06

yz10


People also ask

How can write hover function in jQuery?

The hover() is an inbuilt method in jQuery which is used to specify two functions to start when mouse pointer move over the selected element. Syntax: $(selector). hover(Function_in, Function_out);

Is hover deprecated in jQuery?

hover() is deprecated #66.

What is the jQuery equivalent of Onmouseover?

jQuery mouseover() Method Note: Unlike the mouseenter event, the mouseover event triggers if a mouse pointer enters any child elements as well as the selected element. The mouseenter event is only triggered when the mouse pointer enters the selected element. See the example at the end of the page for a demonstration.

How do you hover in Javascript?

The hover() method specifies two functions to run when the mouse pointer hovers over the selected elements. This method triggers both the mouseenter and mouseleave events.


2 Answers

Try mouseenter and mouseleave

$('.someClass li:first').mouseenter().mouseleave(); 

From jQuery docs

Calling $(selector).hover(handlerIn, handlerOut) is shorthand for: $(selector).mouseenter(handlerIn).mouseleave(handlerOut);

DEMO: http://jsfiddle.net/skram/wh5N9/

Try below to check for an class that would have added in hover

$("#test").mouseenter(function() {     console.log('Has class hover ' + $(this).hasClass('ui-state-hovered')); }).mouseleave(function() {     console.log('Has class hover ' + $(this).hasClass('ui-state-hovered')); }) 

Make sure the above is registered after .hover

DEMO: http://jsfiddle.net/skram/wh5N9/2/

like image 95
Selvakumar Arumugam Avatar answered Oct 06 '22 03:10

Selvakumar Arumugam


Shorthand set mouseenter/mouseleave events

$(".someClass li:first").hover(   // Mouse Over   function(){     $(this).addClass("ui-state-hovered");   },   // Mouse Out   function(){     $(this).removeClass("ui-state-hovered"); }); 

EDIT

Set event mouseenter

$(".someClass li:first").mouseenter(function(){     $(this).addClass("ui-state-hovered");   } 

Set event mouseleave

$(".someClass li:first").mouseleave(function(){     $(this).removeClass("ui-state-hovered"); }); 

To simulate mouseover:

$(".someClass li:first").trigger("mouseenter"); 

To simulate mouseout:

$(".someClass li:first").trigger("mouseleave"); 

To check for a class:

$(".someClass li:first").hasClass("ui-state-hovered"); 

To return true if has a class:

function checkClass(elem, class){   return $(elem).hasClass(class); }; 

EDIT 2

I've never use Konacha before, but if I were to take a stab at it using this guide at solitr.com as my guide, I'd say:

HTML

<div id="testDiv" class="foo">Some Text</div> 

jQuery

checkClass = function(elem, class){     return $(elem).hasClass(class); }; 

Konacha

describe('checkClass', function() {     it('should be true if elem has class', function() {         checkClass("#testDiv", "foo").should.be.true;         checkClass("#testDiv", "bar").should.be.false;     }); }); 
like image 45
DACrosby Avatar answered Oct 06 '22 02:10

DACrosby