user3024814
user3024814

Reputation: 246

Access object within event listener

How can I access object foo? I want to pass it to the event listener. Do I have to make foo global?

function test(){
  var foo = {
    bar:2
  }
}
$('#bla').on('click',function(){
  foo.bar = 5;
});

Upvotes: 0

Views: 58

Answers (1)

Som
Som

Reputation: 598

Try Following

var foo={};
test();
function test(){
  foo.bar=2  ;
}
$('#bla').on('click',function(){
  foo.bar = 5;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="test" id="bla"/>

Upvotes: 1

Related Questions