Watch for form element value change

V

VA

Mozilla-based browsers have watch and unwatch methods to detect change
in value of form elements (or any Javascript variables really). IE
doesn't support this.

Is there a a reliable cross-browser way alternative to this? Something
that will let me detect a change in value for a form element and
trigger my own callback function upon change?

Hopefully, something that is generic enough to be called as
"MyWatch(element)"!

Thanks for any help or pointers.
 
M

Michael Winter

Mozilla-based browsers have watch and unwatch methods to detect
change in value of form elements (or any Javascript variables
really). IE doesn't support this.

Nor do most browsers.
Is there a a reliable cross-browser way alternative to this?

The change event?
Something that will let me detect a change in value for a form
element and trigger my own callback function upon change?

The change event?
Hopefully, something that is generic enough to be called as
"MyWatch(element)"!

A function could be written to attach a predefined listener to a given
element. For instance:

function myListener() {
/* Do stuff here, using this operator to refer to element. */
}

function watchControl(ref) {
ref.onchange = myListener;
}

Mike
 
V

VA

Michael: Yes, I am aware of the onChange event but that would be fired
only when the user manually enters the field, changes its value and
steps out of it.

I am interested in firing my own callback function whenever the *value*
of the item changes, regardless of how it is changed. Either manually
by the user or by some other function by doing, for instance,
document.getElementById('myelement').value="newvalue"

[The Mozilla "watch" method does exactly this which is why I mentioned
it]

Help? Thanks.
 
M

Martin Honnen

VA wrote:

I am interested in firing my own callback function whenever the *value*
of the item changes, regardless of how it is changed.

IE in its DOM has onpropertychange as an event handler to be fired if
properties of a DOM element are changed:
<http://msdn.microsoft.com/library/d...r/dhtml/reference/events/onpropertychange.asp>
It will be fired when various properties are changed, to find out
whether the value property has been changed you then need to check
event.propertyName
 
M

Michael Winter

On 24/12/2005 23:52, VA wrote:

[snip]
Either manually by the user

Use the change event here,
or by some other function by doing, for instance,
document.getElementById('myelement').value="newvalue"

and call the listener here yourself. I admit that it's not a very nice
solution, but it's about as cross-browser as you're going to get.

[snip]

Mike
 
V

VA

Michael said:
and call the listener here yourself

But that's the whole point. The value of the item is changed in many
different places by many different scripts, having to hunt down each
instance of this change and manually calling the onchange listener is
not feasible. Hence, my quest to find an automated way to do this
regardless of how/where the value is changed.

onPropertyChange sounds good, but it is IE-only and even there it
doesn't quite work properly. The example given on that MSDN page Martin
pointed me to doesn't work. It throws JS errors like (oProp not
defined, oSelect is not an object and stuff).

Looks like there is no good cross-browser solution to this problem.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top