Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8038649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:18:09+00:00 2026-06-05T03:18:09+00:00

Having a scope issue problem with a $.each loop in jQuery . How can

  • 0

Having a scope issue problem with a $.each loop in jQuery. How can I get a global variable in a function to set in a loop or at least pass something out of it?

var some_function = function() {

    // false by default
    var something = false;

    $.each(array, function(key, val) { 
       if (val == 'something')
       {
         // even if one item evaluates true I need to check outside of the loop
         something = true;
       }
    });

    if (something == true)
    {
       // do something else, but always false
    }
}

Since I’m needing to evaluate all items in the array, and if only one is true, then do something additional, outside of the $.each.

Update

$(document).ready(function () {
    something(); 
    $(':radio').trigger('change');
)};

Ok, so this is the actual code. It’s alerting ‘false’ at the bottom and then alerts ‘hello’ twice, as if it’s going in reverse order.

var something = function() {   

        var q_radios = {
            'radio1'        : 'radio1_selector', 
            'radio2'        : 'radio2_selector',  
        };
        var show_me = false;

        $.each(q_radios, function(name, q_selector) {  
            $('input:radio[name=' + q_selector + ']').change(function() {  
                    show_me = true; 
                    alert('hello'); 
            }); 
        });   

        if (show_me == true)
        {
            alert('yes');
        }
        else
        {
            alert('false');
        }

};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:18:10+00:00Added an answer on June 5, 2026 at 3:18 am

    More or less, exactly what you have now … you just need to pass the data into the function (by letting it accept an argument). See a live example.

    var some_function = function(array) { 
    
        // false by default
        var something = false;
        $.each(array, function(key, val) {
            if (val == 'something') {
                something = true;
            }
        });
    
        if (something == true) {
            alert("There was a something");
        } else {
            alert("There wasn't a something");
        }
    };
    
    some_function([1,2,3]);
    some_function([1,"something",3]);
    ​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CoffeeScript newbie here. I'm having a weird problem with the scope of a function
I am having an issue with a variable being labeled as out of scope.
I'm having an issue with scope in an if statement, at least, I'm pretty
I have read in various places that having variables with global scope, i.e. a
Im having a bit of an issue with public -v- private variables and scope.
I am having a curiously weird problem, here is my JS code function ValidateUser(username)
I'm having a problem whereby a loop which is instantiating two objects, which in
I'm having an issue with article Tags where a user can write an article
I am having the issue of my below shown lines not storing the variable
I'm having trouble spotting an issue where this jQuery menu breaks when hovering between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.