Reputation: 399
I have the following code, and I am assigning foobar inside the closure?? but I can't seem to get the value of out the scope.
var foobar;
$("#something").change(function(){
var foobar = $('#something').find(":selected").val();
});
foobar
Upvotes: 0
Views: 63
Reputation: 1336
Dont use var foobar
inside closure. Use like this
var foobar;
$("#something").change(function(){
foobar = $('#something').find(":selected").val();
});
alert(foobar);
Upvotes: 1
Reputation: 124
define foobar outside the function.
var foobar;
$("#something").change(function(){
foobar = $(this).find(":selected").val();
});
alert(foobar);
Upvotes: 0
Reputation: 12085
You already declared a variable outside it was fine . if you again declare it inside the function mean. so it can be accessed inside the function only .
var foobar;
$("#something").change(function(){
foobar = $('#something').find(":selected").val();
});
console.log(foobar);
Upvotes: 4