Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get old value in onchange event function

Status
Not open for further replies.

RottPaws

Programmer
Mar 1, 2002
478
US
Is it possible to retrieve the old value for a text input control in a function that is run by an onchange or onblur event?

I've got a series of records that are retrieved and when a field in one record is changed, I'd like to loop through the remaining form elements and update any other records with the same old value to the new value.

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
You could loop through them using javascript when the page has loaded and set a custom attribute on the input elements - set to the same as the value attribute. Your code could then test the custom attribute against the current value element, and likewise could loop and update other "like" values.

Does this make sense?

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
How do you store the information on the page? Do you have it in an array? If so you could have your onchange event pass the index number in the array that the data resides in.
Your function would look up the old value in the array and then know what the old and new values are.



Paranoid? ME?? WHO WANTS TO KNOW????
 
You should check out the "defaultValue" property of the text inputs. It contains the value the input has when it is delivered to the page. It was made for the job ;o)

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
defaultValue...... I was searching for oldvalue ("grew up" in MS Access), originalValue, startValue....

I knew there had to be something like that.

Thanks a bunch!

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
For anybody who's interested, here's what I ended up with.

I call the function in the onchange() event, sending the element's ID and name.
Code:
<INPUT type="text" name="carrier" id="carrier2" value="MCIMETRO, ATS, INC." size="45" onchange="controlUpdate(this.id, this.name)">

Then I loop the the elements of the form, updating any controls with the same name and the default value of the one that was changed.
Code:
//Function to update fields when one is changed
function controlUpdate(updatedElement, elementName){

    var newValue;
    var oldValue;

    newValue = document.submit_form.elements[updatedElement].value;
    oldValue = document.submit_form.elements[updatedElement].defaultValue;

    //alert ("Element: " + updatedElement + "\nOriginal Value: " + oldValue + "\nNew Value: " + newValue);

    for(e=0; e<document.submit_form.length; e++){

		if (document.submit_form.elements[e].name == elementName){

            //If the current value is the same as the one that was changed, update it
			if (document.submit_form.elements[e].value == oldValue){
			    document.submit_form.elements[e].value = newValue;
			}
		}
    }
}

_________
Rott Paws

...It's not a bug. It's an undocumented feature!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top