By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

binding an event to a variable.

P: n/a
Daz
Hi everyone.

Another simple question which should hopefully warrent a simple answer.

I would like a function to be run whenever a variable is updated. I
have tried binding the onchange event to it, and it doesn't work. I
think I understand why, however, I would like to know if there is
anything that I can bind to my variable that will fire the specified
function whenever the value changes.

Is it possible to do this, or would I have to make a class for it?

Many thanks.

Daz.

Nov 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Daz wrote:
Hi everyone.

Another simple question which should hopefully warrent a simple answer.

I would like a function to be run whenever a variable is updated. I
have tried binding the onchange event to it, and it doesn't work. I
think I understand why, however, I would like to know if there is
anything that I can bind to my variable that will fire the specified
function whenever the value changes.

Is it possible to do this, or would I have to make a class for it?
You could make a set function

var foo;

function setFoo(newValue) {
foo = newValue;
someFunction();
}

You could even use a closure and make it so that the foo variable is
hidden and you are forced to use the setFoo() function.

var setFoo = (function(){
var foo;
return function(newValue) {
foo = newValue;
someFunction();
};
})();

Now of course you don't have access to the foo variable to read it but
you can extend this hidding idea to an object that has get and set
methods

var foo = (function(){
var f;
return {
set: function(newValue) {
f = newValue;
someFunction();
},

get: function() {
return f;
}
};
})();
now you can do

foo.set(10.43);
var a = foo.get();

Peter

Nov 14 '06 #2

P: n/a
ASM
Daz a écrit :
Hi everyone.

Another simple question which should hopefully warrent a simple answer.

I would like a function to be run whenever a variable is updated. I
have tried binding the onchange event to it, and it doesn't work. I
think I understand why, however, I would like to know if there is
anything that I can bind to my variable that will fire the specified
function whenever the value changes.

Is it possible to do this, or would I have to make a class for it?
<script type="text/javascript">
var report='';
</script>

<a href="#" onclick="report='Daz';return false;">update Daz</a>
<a href="#" onclick="report='ASM';return false;">update ASM</a>
<a href="#" onclick="alert(report);return false;">see report</a>
Variante :

<a href="#" onclick="if(typeof what != 'undefined')alert(what);
else alert('what\'s the matter ?'); return false;">see what</a>
<a href="#" onclick="what='Daz';return false;">update Daz</a>
<a href="#" onclick="what='ASM';return false;">update ASM</a>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Nov 14 '06 #3

P: n/a
Daz

Peter Michaux wrote:
Daz wrote:
Hi everyone.

Another simple question which should hopefully warrent a simple answer.

I would like a function to be run whenever a variable is updated. I
have tried binding the onchange event to it, and it doesn't work. I
think I understand why, however, I would like to know if there is
anything that I can bind to my variable that will fire the specified
function whenever the value changes.

Is it possible to do this, or would I have to make a class for it?

You could make a set function

var foo;

function setFoo(newValue) {
foo = newValue;
someFunction();
}

You could even use a closure and make it so that the foo variable is
hidden and you are forced to use the setFoo() function.

var setFoo = (function(){
var foo;
return function(newValue) {
foo = newValue;
someFunction();
};
})();

Now of course you don't have access to the foo variable to read it but
you can extend this hidding idea to an object that has get and set
methods

var foo = (function(){
var f;
return {
set: function(newValue) {
f = newValue;
someFunction();
},

get: function() {
return f;
}
};
})();
now you can do

foo.set(10.43);
var a = foo.get();

Peter
I suspected I might have had to do that. Such a pity. Many thanks for
your input, however.

All the best.

Daz.

Nov 14 '06 #4

P: n/a
Daz


On Nov 14, 5:28 pm, ASM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
Daz a écrit :
Hi everyone.
Another simple question which should hopefully warrent a simple answer.
I would like a function to be run whenever a variable is updated. I
have tried binding the onchange event to it, and it doesn't work. I
think I understand why, however, I would like to know if there is
anything that I can bind to my variable that will fire the specified
function whenever the value changes.
Is it possible to do this, or would I have to make a class for it?<script type="text/javascript">
var report='';
</script>

<a href="#" onclick="report='Daz';return false;">update Daz</a>
<a href="#" onclick="report='ASM';return false;">update ASM</a>
<a href="#" onclick="alert(report);return false;">see report</a>

Variante :

<a href="#" onclick="if(typeof what != 'undefined')alert(what);
else alert('what\'s the matter ?'); return false;">see what</a>
<a href="#" onclick="what='Daz';return false;">update Daz</a>
<a href="#" onclick="what='ASM';return false;">update ASM</a>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Thanks for that ASM. However, the variable won't necessarily be updated
through a click, which is why I was trying to add some kind of onchange
event handler to the variable. I was hoping that JavaScript would have
supported something like that directly as, after all, a variable is an
object, but I guess the event handlers are made to work specifically
with D/HTML elements and not from within the inside of the script,
although some do.

Thanks for your suggestions, though. They were very much appreciated.

Best wishes.

Daz.

Nov 14 '06 #5

P: n/a

Daz wrote:
On Nov 14, 5:28 pm, ASM <stephanemoriaux.NoAd...@wanadoo.fr.invalid>
wrote:
Daz a écrit :
Hi everyone.
Another simple question which should hopefully warrent a simple answer.
I would like a function to be run whenever a variable is updated. I
have tried binding the onchange event to it, and it doesn't work. I
think I understand why, however, I would like to know if there is
anything that I can bind to my variable that will fire the specified
function whenever the value changes.
Is it possible to do this, or would I have to make a class for it?<script type="text/javascript">
var report='';
</script>

<a href="#" onclick="report='Daz';return false;">update Daz</a>
<a href="#" onclick="report='ASM';return false;">update ASM</a>
<a href="#" onclick="alert(report);return false;">see report</a>

Variante :

<a href="#" onclick="if(typeof what != 'undefined')alert(what);
else alert('what\'s the matter ?'); return false;">see what</a>
<a href="#" onclick="what='Daz';return false;">update Daz</a>
<a href="#" onclick="what='ASM';return false;">update ASM</a>

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date

Thanks for that ASM. However, the variable won't necessarily be updated
through a click, which is why I was trying to add some kind of onchange
event handler to the variable. I was hoping that JavaScript would have
supported something like that directly as, after all, a variable is an
object, but I guess the event handlers are made to work specifically
with D/HTML elements and not from within the inside of the script,
although some do.

Thanks for your suggestions, though. They were very much appreciated.

Best wishes.

Daz.
In JavaScript, there is a solid distinction between native Value types
(strings, numbers, booleans) and Reference types (all others, including
objects). What's even more confusing for beginners is the fact that
strings, bools etc. are automatically and silently converted into
String, Boolean etc. objects when they are treated like objects in a
script. So it's easy to program in JS for some time without ever being
aware of this distinction.

In your situation, no, there is no way to attach an "event handler" to
a native type, because native types are not objects. Besides, the
concept of "event handler" is specific to the DOM/client-side
scripting, and not at all to the JavaScript/ECMAScript core language.

That doesn't mean that there isn't a way to solve your problem in
JavaScript, just that the first approach you thought of isn't going to
work. Mr. Michaux above had some good suggestions, but yes, any
solution to this is going to involve creating an object of some sort.
Not to worry! JavaScript objects are easy (if inconsistently
documented on the Web), and it's worth the time to learn something
about them.

Nov 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.