kael.shipman Posted July 2, 2007 Share Posted July 2, 2007 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 Quote Link to comment Share on other sites More sharing options...
Wildbug Posted July 2, 2007 Share Posted July 2, 2007 Save an original array, then compare it with your working array on each potential change. I'm not sure that you can do "if (array1==array2)..." directly; you might have to compare each element, but it's still pretty fast. Quote Link to comment Share on other sites More sharing options...
kael.shipman Posted July 3, 2007 Author Share Posted July 3, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.