Faking onchange

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Faking onchange

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Objects already have access to their own assets using "this."
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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'?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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());
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
Post Reply