Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery $.each variable Scope

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');
        }

};
like image 617
wesside Avatar asked Jun 01 '12 15:06

wesside


People also ask

What is each() in jQuery?

jQuery Misc each() Method The each() method specifies a function to run for each matched element. Tip: return false can be used to stop the loop early.

How to break jQuery each loop?

To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.

How to iterate array using jQuery?

Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.

How to use jQuery global variable?

The jQuery global variable is a variable that is declared outside a function and which can be accessed from any function. The variable declared outside a function becomes a global variable and it gets the global scope, which means that the variable can be accessed in all the scripts and functions on a page.


1 Answers

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]);
​
like image 149
Quentin Avatar answered Nov 11 '22 12:11

Quentin