how do I get the previous value

Discussion in 'JavaScript' started by tllcll, Oct 23, 2005.

  1. #1
    Hi,

    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
     
    tllcll, Oct 23, 2005 IP
  2. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are loads of ways to achieve this, let me know why you are wanting to do this and I can point you in the right direction.

    Also do you use a database to store the customer number and how is the form processed (php/asp)?
     
    dave487, Oct 24, 2005 IP
  3. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You could always use a hidden form field to keep the old value:

    
    <form name="valueform">
    <input type="hidden" name="oldvalue" value="">
    <input type="text" name="myvalue" value="" onblur="saveOldValue(this.form);">
    </form>
    
    Code (markup):
    Anytime the user changes the myvalue textbox above and moves from the field, the saveOldValue function below will display an alert with the old value.

    
    <script language="Javascript" type="text/javascript">
    function saveOldValue(inFrm) {
        if ( inFrm.oldvalue.value != '' ) alert('Old Value: '+inFrm.oldvalue.value);
        inFrm.oldvalue.value = inFrm.myvalue.value;
    }
    </script>
    
    Code (Javascript):
    Granted, all of this is client-side, and probably doesn't help you if you wish to display a value a user has submitted to a server-side script. You can always pre-fill the oldvalue textbox with a value from the server though.

    Hope this helps a little.
     
    durango, Oct 26, 2005 IP
  4. cyclinder

    cyclinder Peon

    Messages:
    29
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ...and you should bind the old value with the element you are watching (such as text edit box). like:

    text_el.old_value = value

    so that you could easy pick up it in the future
     
    cyclinder, Nov 25, 2005 IP