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.
JavaScriptjQuery

Avatar of undefined
Last Comment
Julian Hansen
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
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!
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

You are welcome.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo