Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Is there an onUpdate in JS?

    I've got a form with several different elements where the values can be updated in a couple of ways: the user can manually enter in values to the field, or (if present in the DB) can push a button and have the field values get updated automatically. I know I can add one function to each element's onChange and a separate function to the button that itterates thru the different fields, but I thought it would be cleaner if I only had one function to worry about. Furthermore, I thought I might be able to make one event listener targeting 'onUpdate' events... Is this possible?

  • #2
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Pyth007
    I've got a form with several different elements where the values can be updated in a couple of ways: the user can manually enter in values to the field, or (if present in the DB) can push a button and have the field values get updated automatically. I know I can add one function to each element's onChange and a separate function to the button that itterates thru the different fields, but I thought it would be cleaner if I only had one function to worry about. Furthermore, I thought I might be able to make one event listener targeting 'onUpdate' events... Is this possible?

    Nope sorry JS does not include an onUpdate. But what you can do is run the onchange command when you programmatically change the value.

    Code:
    var obj = document.getElementById('your_element_here');
    if ( obj ) {
    	obj.value = 'your new value here';
    	if ( obj.onchange ) obj.onchange();
    }
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #3
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanx... I forgot that events could also be called instead of triggered... That might work well... then I could attach an event listener/handler to onChange instead...

  • #4
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Argh! It's not working the way I expect...

    I've registered an onChange Listener / handler to the elements (the handler just has an alert() stub in the function), and this works fine if the user manually enters in info to the field.

    I've also got the automatic-entry button calling onchange() as it comes across one of these fields with onChange() events. However this does not work correctly (or at least the way I think it should)...

    Some of the fields have their own onChange functions (ie to verify the field-entry).... pushing the button will trigger these, just not the general handler that I attached to all elements. I though attaching event handlers would prevent one function over-riding another (and I don't think the element-specific one is over-riding the general one, since the manual entry will trigger both!) Any ideas?

    BTW: I'm using IE6, but I'm using a cross-browser function to attach the events. I haven't tried using IE's fireevent(), as I'd like to stay away from being too IE-specific, especially for handling JS events.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •