Prem Paryani
Prem Paryani

Reputation: 113

jQuery: Automatically trigger hover

I have a hover mousein mouseout setup as follows for a list item:

$("#main-nav li a").hover(function() {
                $el = $(this);
                leftPos = $el.position().left;
                newWidth = $el.parent().width();
                $magicNav.stop().animate({
                    left: leftPos,
                    width: newWidth
                });
            }, function() {
                t1 = $(".current-menu-item a").position().left;
                t2 = $(".current-menu-item a").parent().width();
                $magicNav.stop().animate({
                    left: t1,
                    width: t2
                });    
            });

And i want to automatically trigger a hover on '.current-menu-item a' as soon as someone enters the website or the page is loaded.

At the moment, i use $(".current-menu-item a").trigger('hover'); and it doesn't work.

Help?

Upvotes: 5

Views: 24777

Answers (4)

Sumit Kumar
Sumit Kumar

Reputation: 1902

$('selector').trigger('eventName');

Upvotes: 0

CKKiller
CKKiller

Reputation: 1422

you need to use mouseover not hover like so

$(".current-menu-item a").trigger('mouseover');

Upvotes: 0

Uttara
Uttara

Reputation: 2532

use this

$(document).ready(function(){
    $(".current-menu-item a").mouseover();
});

or

$(window).load(function(){
    $(".current-menu-item a").mouseover();
});

Upvotes: 9

Mitya
Mitya

Reputation: 34596

$(".current-menu-item a").trigger('mouseenter');

Hover is not a real event (it's a contrived one by jQuery, made up of mouseenter and mouseleave). In any case, it is a two-stage process, so not logically triggerable.

Upvotes: 2

Related Questions