Search code examples
jqueryfunctiondelegatesundefinedready

what is wrong with this jquery delegate code?


function onBodyLoad()
{       
    document.addEventListener("deviceready",onDeviceReady,false);
    //for testing in safari only, comment out for deploy to iphone with phonegap
    $(document).ready(function(){ 
        loadPage('mainmenu', 'Home');

        $('#menus').toggle(); 

        //I have a nested div in this index.html file with id=menubottbutt  
        $("div").delegate("#menubottbutt","click",toggleMenu());

                $("div").delegate(".menuitem","click",toggleMenu());
    });


}

In the body element I have this

When this runs and I hit the delegate line I get the error ** TypeError: 'undefined' is not a function (evaluating'$("div").delegate("#menubottbutt","click",toggleMenu())')


And of course I have the method toggleMenu defined above and it works when called from elsewhere. It looks like this `function toggleMenu() {

$('#menus').toggle('slow'); 

$('#menubottbutt').toggleClass('pressed');

} `

Thanks

PS this is what i have so far: in my onBodyLoad I will use $("#menubottbutt").click(function(){toggleMenu();}); and elsewhere at opportune places I will call: $(".menuitem").click(function(){toggleMenu();}); this seems to address my issues but it would probably be better if i could get delegate to work.


Solution

  • try

     $("div").delegate("#menubottbutt","click",function(){toggleMenu();});
     $("div").delegate(".menuitem","click",function(){toggleMenu();});
    

    third parameter has to be function name

    either is good

    • "toogleMenu()"
    • "toggleMenu"
    • function(){toggleMenu();} // this one is recommended