Watch for form element value change

Discussion in 'Javascript' started by VA, Dec 24, 2005.

  1. VA

    VA Guest

    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.
     
    VA, Dec 24, 2005
    #1
    1. Advertising

  2. On 24/12/2005 18:39, VA wrote:

    > 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

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Dec 24, 2005
    #2
    1. Advertising

  3. VA

    VA Guest

    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.
     
    VA, Dec 24, 2005
    #3
  4. 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/default.asp?url=/workshop/author/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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 25, 2005
    #4
  5. 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

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
     
    Michael Winter, Dec 26, 2005
    #5
  6. VA

    VA Guest

    Michael Winter wrote:
    > > or by some other function by doing, for instance,
    > > document.getElementById('myelement').value="newvalue"

    >
    > 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.
     
    VA, Dec 26, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rod  Snyder

    moving from form element to form element

    Rod Snyder, May 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    409
    Vidar Petursson
    May 29, 2004
  2. Ajit Goel
    Replies:
    0
    Views:
    434
    Ajit Goel
    Apr 16, 2007
  3. Ajit Goel
    Replies:
    0
    Views:
    1,214
    Ajit Goel
    Apr 16, 2007
  4. HANM
    Replies:
    2
    Views:
    723
    Joseph Kesselman
    Jan 29, 2008
  5. OccasionalFlyer
    Replies:
    6
    Views:
    248
    Garrett Smith
    Jul 29, 2009
Loading...

Share This Page