6

Following is my javascript function, I want to use variable selected outside function, but I am getting selected not defined error in console of inspect element. window.yourGlobalVariable is not solving my problem.

function showMe(pause_btn) {
    var selected = [];
    for (var i = 0; i < chboxs.length; i++) {
        if (chboxs[i].checked) {
            selected.push(chboxs[i].value);
        }
    }
}
Ahmed Syed
  • 1,179
  • 2
  • 18
  • 44
  • "stackoverflow javascript global variables" -> http://stackoverflow.com/questions/4862193/javascript-global-variables – royhowie Apr 18 '15 at 08:36
  • possible duplicate of [Define global variable in a JavaScript function](http://stackoverflow.com/questions/5786851/define-global-variable-in-a-javascript-function) – royhowie Apr 18 '15 at 08:37
  • @rowhowie I edited my question window.yourGlobalVariable is not solving my problem. – Ahmed Syed Apr 18 '15 at 09:35
  • @MujahedAKAS: It would solve the problem you've described, assuming a browser environment. If you provide more context, we may be able to help you solve it in a way that doesn't rely on one of JavaScript's biggest bugs (since fixed by strict mode). – T.J. Crowder Apr 18 '15 at 09:38

7 Answers7

13

If you really want it to be global, you have two options:

  1. Declare it globally and then leave the var off in the function:

    var selected;
    function showMe(pause_btn) {
        selected = [];
        for (var i = 0; i < chboxs.length; i++) {
            if (chboxs[i].checked) {
                selected.push(chboxs[i].value);
            }
        }
    }
    
  2. Assign to a window property

    function showMe(pause_btn) {
        window.selected = [];
        for (var i = 0; i < chboxs.length; i++) {
            if (chboxs[i].checked) {
                selected.push(chboxs[i].value); // Don't need `window.` here, could use it for clarity though
            }
        }
    }
    

    A properties of window are global variables (you can access them either with or without window. in front of them).

But, I would avoid making it global. Either have showMe return the information:

function showMe(pause_btn) {
    var selected = [];
    for (var i = 0; i < chboxs.length; i++) {
        if (chboxs[i].checked) {
            selected.push(chboxs[i].value);
        }
    }
    return selected;
}

...and then where you need it:

var selected = showMe();

...or declare it in the scope containing showMe, but not globally. Without context, that looks exactly like #1 above; here's a bit of context:

(function() {
    var selected;
    function showMe(pause_btn) {
        selected = [];
        for (var i = 0; i < chboxs.length; i++) {
            if (chboxs[i].checked) {
                selected.push(chboxs[i].value);
            }
        }
        return selected;
    }

    // ...other stuff that needs `selected` goes here...
})();

The outer anonymous function is a "scoping function" which means that selected isn't global, it's just common to anything in that function.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Instead of assigning it to the window object or declaring it outside of the function, I would recommend creating your own object outside of the function, then assigning variables from there. This avoids cluttering the window object and puts all of your global variables in one place, making them easy to keep track of. For example,

var globalObject {}
function MyFunction {
  globalObject.yourVariableName=what your variable is
}

1

Do this:

var selected;

function showMe(pause_btn) {
    selected = [];
    for (var i = 0; i < chboxs.length; i++) {
        if (chboxs[i].checked) {
            selected.push(chboxs[i].value);
        }
    }
}

You can actually skip the var selected; line but I prefer declaring my variables.

Juan
  • 15,274
  • 23
  • 105
  • 187
1

Dont use this;

 selected = [];

it is a bug of javascript

window.selected = []; 

inside your function.

0

You could define the array called selected in the scope that the function called showMe is defined.

In terms of code:

var selected = [];

function showMe(pause_btn) {
    for (var i = 0; i < chboxs.length; i++) {
        if (chboxs[i].checked) {
            selected.push(chboxs[i].value);
        }
    }
}
Christos
  • 53,228
  • 8
  • 76
  • 108
0
var selected = [];
function showMe(pause_btn) {

    for (var i = 0; i < chboxs.length; i++) {
        if (chboxs[i].checked) {
            selected.push(chboxs[i].value);
        }
    }
}
Jasmin Mistry
  • 1,459
  • 1
  • 18
  • 23
0

If you declare selected as a property on the window object, you will be able to access it from anywhere else.

function showMe(pause_btn) {
  window.selected = [];
  for (var i = 0; i < chboxs.length; i++) {
    if (chboxs[i].checked) {
      selected.push(chboxs[i].value);
    }
  }
}
Dan Prince
  • 29,491
  • 13
  • 89
  • 120