Page 1 of 1

Faking onchange

Posted: Sun May 27, 2007 3:05 pm
by superdezign
I was always under the impression that when I change the value of an input box, I would cause an 'onchange' event. Well, turns out that I was mistaken, so now I am stumped.

I wrote a script that changes a value in a textbox, but I don't know how to retrieve that value after it's changed, and use it elsewhere on the page. Like:

Code: Select all

inputbox1.onchange = function(){inputbox2.value = inputbox1.value;}
Is there a way to fake an event? Better yet, is there a way to give an object it's own events?

Posted: Sun May 27, 2007 4:22 pm
by feyd
Objects already have access to their own assets using "this."

Posted: Sun May 27, 2007 4:26 pm
by superdezign
Okay, I've just realized that the fake script that I wrote as an example is fully functional. :oops:

The 'onchange' event works fine when the input is changed, and then enter is pressed, but if I change the value dynamically, it isn't recognized.

Therein lies the problem.

Posted: Sun May 27, 2007 4:27 pm
by superdezign
feyd wrote:Objects already have access to their own assets using "this."
You mean when I wrote 'inputbox1.value' instead of 'this.value'?

Posted: Sun May 27, 2007 4:33 pm
by feyd
Javascript will not automagically fire the event. You will need to fire it yourself during your updating.

Is there a reason this code is merely copying the content from one field to another?

Posted: Sun May 27, 2007 4:40 pm
by superdezign
I have a class that creates a slider that updates an input value when it's slid, and the input value updates the slider when it's changed. I have three sliders and I'm using them to create a color using their values for the color's r, g, and b values. At least, that's what I'm trying to do.

The problem is getting my Slider class to interact with my ColorPreview class. The ColorPreview class has a ChangeColor(r, g, b) function, and I'd like to access the function.

I tried giving the Slider object a reference to the ColorPreview object, and vice versa, but I always run into problems with the "this" keyword when having them access each others' functions.

Posted: Sun May 27, 2007 4:50 pm
by feyd
"this" only refers to the current object. The object's properties (and methods) are accessed via the object's identifier. This is normally a parameter to the method via data passing.

However, calling the onchange event is more proper I would think.

Posted: Sun May 27, 2007 4:57 pm
by superdezign
feyd wrote:"this" only refers to the current object. The object's properties (and methods) are accessed via the object's identifier. This is normally a parameter to the method via data passing.
Precisely.
feyd wrote:However, calling the onchange event is more proper I would think.
Any idea how I can pull off:

Code: Select all

var r = new Slider();
var prev = new ColorPreview();
r.onchange = prev.ChangeRed(r.GetValue());

Posted: Sun May 27, 2007 5:02 pm
by feyd

Posted: Sun May 27, 2007 5:08 pm
by superdezign
I'm still reading it, but I'm not sure if I'm getting it correctly.

It like... entwining two classes together that should be able to interact?