Jump to content

Listening on a Variable


kael.shipman

Recommended Posts

Hey,

 

I'm looking to track changes to an array that's driving a certain page, but I can't figure out a clean way to do it. I thought maybe I could attach an onchange listener to my variable, but it didn't work. Does anyone know how to do something like that? Here's a little example:

 

<html>
<head>
<script type="text/javascript">
var chngs = new Array(3);
chngs[0] = "Cha";
chngs[1] = "Fah";
chngs[2] = "Lah";
chngs.onchange = function() {
  alert("Changed!");
}
</script>
</head>
<body>
<button onclick="chngs[2] = 'Nah';">Change Chngs[2]</button>
<button onclick="chngs[3] = 'Gah';">Add Change</button>
</body>
</html>

 

I'd like to get an alert when I push either button, but nothing's happening.

 

Thanks for any help you can offer.

 

-kael

Link to comment
https://forums.phpfreaks.com/topic/58140-listening-on-a-variable/
Share on other sites

That would work and all, but I'm really just wondering if there's a passive listener that I can set up so I don't have to call a function manually every time I change the variable.

 

The advantage would be as follows:

 

With Listener

....
Functions
....

<body>
<input onchange="array[0] = this.value;" /> <!-- verifyChanges() gets called when this changes -->
<input onchange="array[1] = this.value;" />
etc...
</body>

 

Without Listener

....
Functions
....

<body>
<input onchange="array[0] = this.value; verifyChanges();" />
<input onchange="array[1] = this.value; verifyChanges();" />
etc...

 

Obviously not a huge deal, but it would be nice to be able to just have verifyChanges() called automatically every time the array changes. I can think of a host of other ways to accomplish this, but I'm just wondering if it is indeed possible to set up a listener on a variable like this.

 

Thanks again,

Kael

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.