Results 1 to 6 of 6

Thread: how to force fire the onChange event

  1. #1
    Join Date
    Mar 2006
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to force fire the onChange event

    I have an item whose value is set by javascript code.
    I want to place an onChange event handler on this item, only problem is that the onChange event handler will not fire if the items value is changed by javascript code.

    Is there anyway of forcing the onChange event to fire??

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes, when you change the element's value using javascript add a line:

    Code:
    document.getElementById('elID').onchange();
    where elID is the element's id.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2006
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    IE
    throws up the following error:
    object doesn't support this property or method

  4. #4
    Join Date
    Mar 2006
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    got it to work....
    I was trying to fire the event on a hidden item (which doesn't work) have changed it to a visible item and it now works.

    Thanks for the help...

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you want to run the event on an invisible element, depending upon the event, you may be able to do so by running the event's code rather than the event itself. Say the onchange event was:

    Code:
    onchange="alert (this.value);"
    In your script that alters that element use this line:

    Code:
    alert(document.getElementById('elID').value);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Yay for old topics! This just totally helped me.

    And firing the event for a hidden <select> element works just fine for me in IE6+.

Bookmarks

Posting Permissions

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