previous value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smiley22
    New Member
    • Jun 2007
    • 54

    previous value

    I would like to know how to check the current value and the previous value entered by user before changes.

    for example, the form will allow user to enter customer number. The first time user enter 10 then change to 22

    customer number : 10 -> change to 22

    how do I get the previous value which is 10 in this case


    thanks...
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    in that case you have to store the values in your app ... it is a good idea to write a kind of utility-function for that purpose ... one that loops through your desired formelements and stores the values of each element within an content-object or something like this. it should create something like the following:

    [CODE=javascript]
    // content object
    var content_obj = {
    element_name_or _id_1: value,
    element_name_or _id_2: value,
    element_name_or _id_3: value,
    element_name_or _id_4: value
    // as much elements as you need
    };
    [/CODE]

    later ... probably in the onchange-event of your elements you check the newvalue against the one of your content-object.

    [CODE=javascript]
    // refer properly to element_name_or _id_1 (here we use the id)
    var ele = document.getEle mentById('eleme nt_name_or_id_1 ');
    if (ele.value != content_obj['element_name_o r_id_1']) {
    // or whatever you want to check
    }
    [/CODE]

    you may set the content_obj[element_name_or _id_1] to newvalue when the check was successful or call your utility-function again so that it stores the actual values and you may compare again old- and newvalues ...

    thats the idea ... hope it helps ...

    kind regards ...

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by smiley22
      I would like to know how to check the current value and the previous value entered by user before changes.

      for example, the form will allow user to enter customer number. The first time user enter 10 then change to 22

      customer number : 10 -> change to 22

      how do I get the previous value which is 10 in this case


      thanks...
      see onchange event fired when the an element loses focus and the value changed.
      so on firing this event the updated value we can get , not the previous value.
      so try this ......

      [code=html]
      <input type = text onfocus = fun1(this) onblur = fun2(this)>
      [/code]

      [code=js]
      var prev_value,cur_ value;
      function fun1(val)
      {
      prev_value = val.value;
      }
      function fun2(val)
      {
      cur_value = val.value;
      }
      [/code]

      i hop it ll help u.
      best of luck

      kind regards,
      dmjpro.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        Originally posted by dmjpro
        see onchange event fired when the an element loses focus and the value changed.
        so on firing this event the updated value we can get , not the previous value.
        so try this ......

        [code=html]
        <input type = text onfocus = fun1(this) onblur = fun2(this)>
        [/code]

        [code=js]
        var prev_value,cur_ value;
        function fun1(val)
        {
        prev_value = val.value;
        }
        function fun2(val)
        {
        cur_value = val.value;
        }
        [/code]

        i hop it ll help u.
        best of luck

        kind regards,
        dmjpro.
        hi

        ... the focus event is a good point to store the value, your'e right, but onblur is not ... onchange is even better ... because you may change the value with pressing enter and then the field don't loose focus and nothing blurs ...

        kind regards ...

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by gits
          hi

          ... the focus event is a good point to store the value, your'e right, but onblur is not ... onchange is even better ... because you may change the value with pressing enter and then the field don't loose focus and nothing blurs ...

          kind regards ...
          that means u say if the value changed without changing it's value then onchange event firing.
          right???
          i just need confirmation.

          kind regards,
          dmjpro.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #6
            Originally posted by dmjpro
            that means u say if the value changed without changing it's value then onchange event firing.
            right???
            i just need confirmation.

            kind regards,
            dmjpro.
            no ... you change the value and press enter ... then no blur-event fires but the onchange do!

            kind regards ...

            Comment

            • dmjpro
              Top Contributor
              • Jan 2007
              • 2476

              #7
              Originally posted by gits
              no ... you change the value and press enter ... then no blur-event fires but the onchange!

              kind regards ...
              oh i seee ..... thanksssssss

              kind regards,
              dmjpro.

              Comment

              Working...