Javascript Alterations

i am wanting to adjust the following javascript from a mouseover event to a page load event, however my attempts to do so have so far failed. So to clarify, when the page loads i want the script to activate. here is the code:

$(function() {
            $('#sdt_menu2 > li').bind('mouseenter',function(){
                var $elem = $(this);
                $elem.find('img')
                     .stop(true)
                     .andSelf()
                     .find('.sdt_wrap')
                     .stop(true)
                     .andSelf()
                     .find('.sdt_active')
                     .stop(true)
                     .animate({'height':'45px'},300,function(){
                    var $sub_menu = $elem.find('.sdt_box');
                    if($sub_menu.length){   
                    }   
                });
9

1 Answer

If I understood the quesiton correct, it's as easy as

$(function(){
            var $elem = $(this);
            $elem.find('img')
                 .stop(true)
                 .andSelf()
                 .find('.sdt_wrap')
                 .stop(true)
                 .andSelf()
                 .find('.sdt_active')
                 .stop(true)
                 .animate({'height':'45px'},300,function(){
                var $sub_menu = $elem.find('.sdt_box');
                if($sub_menu.length){   
                }   
            });

$(handler) (that you used already) actually is a shortcut for $(document).ready(handler).
From the jQuery Documentation on .ready():

All three of the following syntaxes are equivalent:
$( document ).ready( handler )
$().ready( handler ) (this is not recommended)
$( handler )

[Edit]: If the function should still work on $('#sdt_menu2 > li'), the $elem (is this even a valid name? Oo) has to be set to that, of course. So if that'S what you want, substitute

var $elem = $(this);

by

var $elem = $('#sdt_menu2 > li')`
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest