meskerem
meskerem

Reputation: 399

Jquery how to get value assigned inside a function

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

Answers (3)

Sateesh
Sateesh

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

alil rodriguez
alil rodriguez

Reputation: 124

define foobar outside the function.

    var foobar; 
$("#something").change(function(){
    foobar = $(this).find(":selected").val();
 });

 alert(foobar);

Upvotes: 0

JYoThI
JYoThI

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

Related Questions