0
$('#lobSelect').change(function () {
    var selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
    console.log(selectedLob); //prints to the console
});

console.log(selectedLob); //not available here

In the code above i'm using the variable selectedLob to store the value selected from dropdown.

how can i retrieve that value from outside the function?

Also say this function is stored in a file1.js - how can i access this variable from file2.js in a different folder.

Thanks

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Spdexter
  • 923
  • 1
  • 7
  • 19

7 Answers7

2

When you declare a variable, you make it local to the scope of the function it is declared within.

Declare it outside the function:

var selectedLob;

And don't re-declare it inside:

$('#lobSelect').change(function () {
    selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
    console.log(selectedLob); //prints to the console
});

It will still be undefined here:

console.log(selectedLob); //not available here

because it doesn't get a value assigned to it until the change event fires, but it will be accessible in the wider scope after that event has happened.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • +1 The only answer to even mention the fact that `selectedLob` will only be populated after the `change` event. – CodingIntrigue May 14 '14 at 12:18
  • +1 - With @RGraham on this one. – marcjae May 14 '14 at 12:19
  • Tried the above - but still undefiend even after the change event. – Spdexter May 14 '14 at 12:19
  • @Spdexter, how are you logging it after the change event? – marcjae May 14 '14 at 12:20
  • var selectedLob =''; $(function(){ $('#lobSelect').change(function () { selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val(); }); }); console.log(selectedLob); – Spdexter May 14 '14 at 12:22
  • @Spdexter — You're logging it after you *assign the change event handler*, not after the change event happens. – Quentin May 14 '14 at 12:24
  • @Spdexter. It will still be undefined, as the change event hasn't been fired yet. If you would like to get the default value, when you first declare the variable, get the current selected (default) value. var selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val(); – marcjae May 14 '14 at 12:25
  • http://jsbin.com/witikobi/1/edit – Quentin May 14 '14 at 12:27
1

You can use global variable for your task, but this will lead to polluting the global namespace.

The good practise is to use application namespace instead of global namespace

Your file1.js

// your app namespace
var app = app || {};

// on document ready wrapper
$(function () {

$('#lobSelect').change(function () {
    // initialize your variable at app namespace
    app.selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
});

});

Your file2.js

// use the same app namespace or create it
// if not exist yet (in case when file2.js was loaded before file1.js)
var app = app || {}; 
$(function () {

// app.selectedLob is available here

});
muzar
  • 53
  • 1
  • 6
0

var selectedLob is local to $('#lobSelect').change(function () { function not accessible outside

Solution

var selectedLob =''; //declare variable outside so it accessible 
$('#lobSelect').change(function () {
    //assign value here
    selectedLob = $('#mainWrapper').find('select[name="lob-select"]  option:selected').val();
    console.log(selectedLob); //prints to the console
});

console.log(selectedLob);
 //it doesn't get the value as it runs before the change event fires so you get undefined value

Read What is the scope of variables in JavaScript?

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

You can use jQuery.data function to store in $('#lobSelect')

$('#lobSelect').data("yourKey", yourVariable)

And retrieve like this:

var someVariable = $('#lobSelect').data("yourKey")

Or declare the variable outside the function as suggested if you want to use it right after (data will store for a longer time):

var selectedLob;
$('#lobSelect').change(function () {
    selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
    console.log(selectedLob); //prints to the console
});

console.log(selectedLob); 
//accessible here, 
//maybe not elsewhere in your code (depending on your code structure)
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • Can downvoter comment? – GôTô May 14 '14 at 12:20
  • 1
    I can. Changing the storage mechanism wouldn't affect the problem. The issue is where the variable is accessed, rather than solely an issue of scope. – CodingIntrigue May 14 '14 at 12:21
  • 1
    Have you read the question entirely? Spdexter also wants to access this variable from another file, probably not the same scope, I don't see how others answered this point – GôTô May 14 '14 at 12:23
  • 1
    You're right. Missed that last line and was only looking at the example (which obviously isn't representative of the question!). – CodingIntrigue May 14 '14 at 12:25
0

you can also set a global variable inside a function using the following method

window["variableName"] = "some value";

then access it anywhere. for example:

console.log(window["variableName"]);
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
Gal Ziv
  • 6,890
  • 10
  • 32
  • 43
0

First problem you have here is the fact that you are using selectedLob variable outside of the scope it is defined in, so you have to move its declaration one level up so it can be used outside of the function as well (or even better you should restructure your code so this kind becomes unnecessary).

Second problem you have is you declare selectedLob variable inside change event handler and expect it to be defined immediately. To fix this (if you are using JQuery here) you can call your change handler function right after you declare it to initially kick start your variable initialization.

So in summary something like this:

var selectedLob =''; //declare variable outside change handler so it accessible 
$('#lobSelect').change(function () {
    //assign value here
    selectedLob = $('#mainWrapper').find('select[name="lob-select"]  option:selected').val();
    console.log(selectedLob); //prints to the console
}).change(); // This will call your change event handler and hopefully init selectedLob

// Now selectedLob should be have value of selected option (if your DOM was ready...)
console.log(selectedLob);

And at the end I would have to say that you should really try to avoid things like this, try to restructure your JS code in a way that you maybe initialize all things you need in one place after DOM is ready and then to start up your app with some kind of init function passing all it needs or similar. Writing code like this will soon lead into one big pile of mess :)

sbgoran
  • 3,451
  • 2
  • 19
  • 30
-2

Use global variable

 var selectedLob;  // use as a global variable to access outside of the function 
     $('#lobSelect').change(function () {
       selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
       console.log(selectedLob); //prints to the console
     });

if(selectedLob!=undefined){

     console.log(selectedLob); //available here
}
Balachandran
  • 9,567
  • 1
  • 16
  • 26