Element OnReset Event?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    Element OnReset Event?

    Hi,

    On my form, I turn the forecolor blue when an element's value changes by
    comparing a custom "origVal" tag on each element. This works fine.

    Then, when the user resets the form (double escape key), the onreset event
    fires. I then loop through all the elements in the form and change the
    forecolor back to black. This also works. However, it takes a long time to
    loop through this because of the large number of elements on the page.

    If possible, I want to process only the elements that have been reset, not
    all of them. Is there some undocumented event or some method I can use to
    revert the forecolor on only the changed items?

    I already tried adding an "onreset" attribute to each element, but that
    didn't fire. Also, the onchange event does not apply because it only fires
    when a single element loses focus and has a changed value.

    Thanks in advance,

    Eric
  • john henry bonham

    #2
    Re: Element OnReset Event?

    Eric wrote:[color=blue]
    > Hi,
    >
    > On my form, I turn the forecolor blue when an element's value changes by
    > comparing a custom "origVal" tag on each element. This works fine.
    >
    > Then, when the user resets the form (double escape key), the onreset event
    > fires. I then loop through all the elements in the form and change the
    > forecolor back to black. This also works. However, it takes a long time to
    > loop through this because of the large number of elements on the page.
    >
    > If possible, I want to process only the elements that have been reset, not
    > all of them. Is there some undocumented event or some method I can use to
    > revert the forecolor on only the changed items?
    >
    > I already tried adding an "onreset" attribute to each element, but that
    > didn't fire. Also, the onchange event does not apply because it only fires
    > when a single element loses focus and has a changed value.
    >
    > Thanks in advance,
    >
    > Eric[/color]

    The following script assumes origVal is an Array.

    for ( var i = 0 ; i < document.forms[ 0 ].elements.lengt h ; i++ )
    {
    if ( document.forms[ 0 ].elements[ i ].value != origVal[ i ] )
    {
    document.forms[ 0 ].elements[ i ].style.color = '#000000';
    }
    }

    Rob - Avagio IT Services
    <a href="http://www.avagio.co.u k">Avagio IT Services</a>

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Element OnReset Event?

      Eric wrote:[color=blue]
      > On my form, I turn the forecolor blue when an element's value changes by
      > comparing a custom "origVal" tag on each element. This works fine.[/color]

      "origVal" tag?

      For "input" and "textarea" elements, there is a "defaultVal ue" property that
      you should use and should compare against:

      <input
      value="foo"
      onchange="this. style.color = (this.value != this.defaultVal ue? 'blue' :
      '');"><!-- one line! -->

      <textarea>
      onchange="this. style.color = (this.value != this.defaultVal ue? 'blue' :
      '');"></textarea><!-- one line! -->

      (Or do that in an event listener of the "form" element object.)
      [color=blue]
      > Then, when the user resets the form (double escape key), the onreset event
      > fires. I then loop through all the elements in the form and change the
      > forecolor back to black. This also works. However, it takes a long time to
      > loop through this because of the large number of elements on the page.
      >
      > If possible, I want to process only the elements that have been reset, not
      > all of them. Is there some undocumented event or some method I can use to
      > revert the forecolor on only the changed items?[/color]

      You have set them to blue text color, why not test against that? Then
      set the style.color property of that elements to "" to set the default
      text color. You should do that with style.backgroun dColor as well, and
      be sure to set both text *and* background color on change of an element.

      <http://www.w3.org/QA/Tips/color>
      [color=blue]
      > I already tried adding an "onreset" attribute to each element, but that
      > didn't fire.[/color]

      There is no "onreset" attribute for elements other than "form" elements.

      <http://www.w3.org/TR/html4/index/attributes.html


      PointedEars

      Comment

      • Eric

        #4
        Re: Element OnReset Event?

        Thank you both for your responses...ver y helpful. I'm still learning as I go. :-)

        Take care,

        Eric

        Comment

        Working...