Link to home
Start Free TrialLog in
Avatar of elepil
elepil

asked on

Basic question about jQuery

I'm still new to jQuery and still learning. Ever since I started using it, I've been putting all my JavaScript code inside:

$(function() {
    function insideJQuery() {
        // This function calls stuff within this jQuery function, so it has to be here.
    }
}

Open in new window


But now I'm facing a problem where I have an <a> element with href="javascript:insideJQuery()", and I'm getting the error message -- "Uncaught ReferenceError: insideJQuery() is not defined". This is expected because the function is inside another function, and therefore is out of scope.

But I can't really move the insideJQuery() function out of the JQuery's function because that function calls other functions within that scope that use a lot of jQuery; that's why it's there in the first place.

How can I access an inner nested function from the outside? The jQuery function isn't exactly a function I can call and have it return a reference to the inner nested function. What should I do?

Thanks.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of elepil
elepil

ASKER

Excellent responses from both Alexandre Simões and JulianH!

While JulianH provided an excellent workaround for my problem, Alexandre attacked my problem from the root. I come from a classic OOP background with both Java and ActionScript, so I am still in the process of acclimating to the nuances and idiosyncrasies of the PHP and JavaScript world. I am already a heavy proponent and believer of constructor functions, which is what you demonstrated. Your response addressed directly the aspects of JavaScript that I was uncertain of, and I greatly appreciate it!

I thank you both for your high-quality solutions!
You are welcome.