Passing Result from jQuery Embedded function to Parent JavaScript Function For Return?

I don't know why the embedded jQuery function below is not returning the value to my JS var? I do get the expected value in console.log(" >>>> FINAL RESULTS RETURNED : " + results);
function _dtmGetCurrResults(){
	    var results = [];
	    $(function() {
            $('[class*="solid-shape-large-rating-"]').each(function(i, e) {
                var result = this.className.match(/solid-shape-large-rating-(.*?)\s+/);
                results.push(result[1]); 
            });
        results = results.join("|");
        console.log(" >>>> FINAL RESULTS RETURNED : " + results);
		});
		return results;
	};
var myResults = _dtmGetCurrResults();

Open in new window



Thanks!
894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
The way you've defined your internal function:

$(function() {
   ...

Open in new window

... is the shorthand for the dom.ready block in jQuery. This means that execution will be deferred until the DOM is ready. However, your call to _dtmGetCurrResults() will fire as soon as it's hit, which is likely to be before the DOM is ready. You're effectively getting the results of your function BEFORE the internal one has had a chance to run. Basically, it's doing this:

var results = [];
return results
... some time later run $(function() { ... }

Open in new window

You need to change things around a little:

function _dtmGetCurrResults(){
    var results = [];
    $('[class*="solid-shape-large-rating-"]').each(function(i, e) {
        var result = this.className.match(/solid-shape-large-rating-(.*?)\s+/);
        results.push(result[1]); 
    });
    results = results.join("|");
    console.log(" >>>> FINAL RESULTS RETURNED : " + results);
    return results;
};

$(function() {
    var myResults = _dtmGetCurrResults();
}

Open in new window

We've now moved the call to _dtmGetCurrResults() into the dom.ready, so that will fire once the DOM is loaded and it'll return the correct results. Depending on how and when you need to use myResults, you may need to edit the scope.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
894359Author Commented:
Thank you for your help. I'm obviously not a person that uses jQuery!  Much appreciated!
Chris StanyonWebDevCommented:
No worries. Glad I could help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.