javascriptjqueryfunctioninline-scripting

jquery - function not defined - calling this function from inline script


I have the code as shown below. Have removed unwanted code from this, just wrote what was needed. When I call my toggleFunc from inline script in body, it shown in console that this function is not defined. Can anyone please tell me whats wrong with this?

<head>
<script src="~/Client/js/lib/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        var pageInitialize = function () {  

            ..doing something here

            function toggleFunc() { 
               ..doing something more here
            };
        };
        pageInitialize();
    });
</script>
</head>
<body>
<script>toggleFunc()</script>
</body>

Solution

  • Both your functions will not be defined until DOMReady has fired, which will happen after the call to toggleFunc in the body has been run. Also, toggleFunc is within the pageInitialize function, and is therefore not accessible outside pageInitialize.

    Try this:

    <script type="text/javascript">
        var pageInitialize = function () {  
            //..doing something here
        };
        pageInitialize();
    
        function toggleFunc() { 
            //..doing something more here
        };
    </script>