0

I have a variable that's being changed all the time in the course of my application. Can I make an element change (contents, colour, whatever), when the variable changes?

Or do I have to change the element each time I change the variable. Ideally, the result would almost be like binding a change event to the variable...

Thanks

Gausie

Gausie
  • 4,291
  • 1
  • 25
  • 36

4 Answers4

2

look here: Listening for variable changes in JavaScript or jQuery

Community
  • 1
  • 1
david
  • 2,529
  • 1
  • 34
  • 50
1

Look at jQuery custom event support. It may not be a straight forward case in your particular usecase but you can use it to make it work. Here is a decent tutorial.

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
0

I have a variable that's being changed all the time in the course of my application. Can I make an element change (contents, colour, whatever), when the variable changes?

Yes you can change, you know there will be the place where your variable changes, right? That's where you can change an element's content, style etc:

your_variable = 'this var changes here';
$('#your_div_id').css('color', 'green');
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

If you use a property on an object to store your value instead of a variable, you can add a JavaScript watch to that property, and run a function of choice whenever the property is assigned to.

John K
  • 28,441
  • 31
  • 139
  • 229