Capture when a textbox has changed!

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

    #1

    Capture when a textbox has changed!

    Hi!

    Does anybody of you have a good algorithm to capture when a value in a
    textbox is changed? I'm using the keypress/keydown events but they seems to
    be a bit inconsistent (IE and Mozilla). Basically what I want is to set is a
    "isDirty" flag when a user has changed any value in the textbox (or in a
    dropdown) and then if he/she tries to leave the record without saving I want
    to give a save warning. Since I don't want set the isdirty flag if you for
    example klick tab, certain F-buttons, arrows (up/down/left/right) etc I
    tried to capture the keypress/keydown event and ignore specific keycodes.
    Since there are several keycodes that should be ignored I was hoping there
    is a better solution.

    Must support IE6 and NN7+ (mozilla). Since the site is unicode compatible
    the user may add chinese characters for example.

    Regards
    /Hans


  • Michael Winter

    #2
    Re: Capture when a textbox has changed!

    On Wed, 29 Sep 2004 12:12:21 +0200, Hans <sorry@nospam.c om> wrote:
    [color=blue]
    > Does anybody of you have a good algorithm to capture when a value in a
    > textbox is changed?[/color]

    Umm, the change event, perhaps?

    [snip]

    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • harry

      #3
      Re: Capture when a textbox has changed!

      use the onChange() event mate, trying to grab key press's is a bit of a
      nightmare as key's like "del", "backspace" behave differently!


      "Hans" <sorry@nospam.c om> wrote in message
      news:cje1t4$50e $1@green.tninet .se...[color=blue]
      > Hi!
      >
      > Does anybody of you have a good algorithm to capture when a value in a
      > textbox is changed? I'm using the keypress/keydown events but they seems[/color]
      to[color=blue]
      > be a bit inconsistent (IE and Mozilla). Basically what I want is to set is[/color]
      a[color=blue]
      > "isDirty" flag when a user has changed any value in the textbox (or in a
      > dropdown) and then if he/she tries to leave the record without saving I[/color]
      want[color=blue]
      > to give a save warning. Since I don't want set the isdirty flag if you for
      > example klick tab, certain F-buttons, arrows (up/down/left/right) etc I
      > tried to capture the keypress/keydown event and ignore specific keycodes.
      > Since there are several keycodes that should be ignored I was hoping there
      > is a better solution.
      >
      > Must support IE6 and NN7+ (mozilla). Since the site is unicode compatible
      > the user may add chinese characters for example.
      >
      > Regards
      > /Hans
      >
      >[/color]


      Comment

      • Colin McKinnon

        #4
        Re: Capture when a textbox has changed!

        Hans wrote:
        [color=blue]
        > Hi!
        >
        > Does anybody of you have a good algorithm to capture when a value in a
        > textbox is changed? I'm using the keypress/keydown events but they seems[/color]
        <snip>

        Most form elements have a defaultValue property (what the value would be
        changed to if the form was reset).
        [color=blue]
        >
        > Must support IE6 and NN7+ (mozilla). Since the site is unicode compatible
        > the user may add chinese characters for example.
        >[/color]

        Note that the defaultValue property doesn't work properly for hidden input
        elements in Mozilla based browsers.

        HTH

        C.

        Comment

        • Hans

          #5
          Re: Capture when a textbox has changed!

          Thanks I tested the onchange event but it did not fire but I now realize
          that it fires when the control loses the focus and not for every keystroke.

          Thanks
          /Hans


          Comment

          • Hans

            #6
            Re: Capture when a textbox has changed!

            Hi again. The onchange event does not seem to work if I inside a textbox for
            example capture F12 which is setting the value in the textbox to some value.
            When I tab to the next field the onchange event do not fire (i can of course
            set the isdirty flag in the code that is capturing the return key). If I
            type a value in the first textbox and then tab to the second the onchange
            event fire. Is it something strange with my code of capturing the F12 button
            or is the onchange not triggered if the value is changed by javascript code?


            <html>
            <body>
            <script language="javas cript" type="text/javascript">
            function captureF12(evt)
            {
            var keyCode = evt.keyCode ? evt.keyCode : evt.charCode ? evt.charCode :
            evt.which;
            if (keyCode==123)
            document.getEle mentById('a').v alue='Test';
            return true;
            }
            </script>

            <form>
            <input type="text" id="a" onchange="alert ('onchange has fired');"
            onkeydown="capt ureF12(event);" >
            <input type="text" id="b">
            </form>
            </body>
            </html>

            Regards
            /Hans



            Comment

            • Michael Winter

              #7
              Re: Capture when a textbox has changed!

              On Wed, 29 Sep 2004 14:01:08 +0200, Hans <sorry@nospam.c om> wrote:
              [color=blue]
              > Hi again. The onchange event does not seem to work if I inside a textbox
              > for example capture F12 which is setting the value in the textbox to
              > some value.[/color]

              That's because F12 doesn't change the value.

              On my browser (Opera), F12 brings up the Quick Preferences menu. I don't
              think it does anything in IE or Mozilla.

              Why do you think F12 is significant? None of the function keys should be.

              [snip]
              [color=blue]
              > <script language="javas cript" type="text/javascript">[/color]

              The language attribute is deprecated and redundant. Don't use it. Just
              stick with the (required) type attribute.

              [snip]


              --
              Michael Winter
              Replace ".invalid" with ".uk" to reply by e-mail.

              Comment

              • Hans

                #8
                Re: Capture when a textbox has changed!

                > That's because F12 doesn't change the value.
                No I understand that it does not change the value but in my case I capture
                the event and change the value by javascript. But obviously the onchange
                event is not triggered.[color=blue]
                >
                > On my browser (Opera), F12 brings up the Quick Preferences menu. I don't
                > think it does anything in IE or Mozilla.[/color]
                Yes that is why we use F12 to insert the username/timestamp in a field (from
                the windows version of the application users are used to use F5 but that is
                not a good key to use since it is used by many browsers to reload the page).
                [color=blue]
                >
                > Why do you think F12 is significant? None of the function keys should be.
                >
                > [snip]
                >[color=green]
                > > <script language="javas cript" type="text/javascript">[/color]
                >
                > The language attribute is deprecated and redundant. Don't use it. Just
                > stick with the (required) type attribute.
                >[/color]
                Yes I know. We supported NN4.7x before this release so I had to stick with
                the old language attribute.

                Once again thanks for your help.

                regards
                /Hans


                Comment

                Working...