views:

169

answers:

4

Hi all,

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

A: 

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
Well yeah, that's what I'll have to settle with. But it's not ideal...
Gausie
+1  A: 

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
+1  A: 

look here: http://stackoverflow.com/questions/1759987/detect-variable-change-in-javascript

david
That'll do. It's certainly given me the right ideas.
Gausie
A: 

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