| Author |
problem with onchange event
|
dhriti joshi
Ranch Hand
Joined: Aug 13, 2002
Posts: 82
|
|
Hi, I have several input fields in my jsp.I want the user to change only any one input field and then submit.I set a flag at onchange event.but what happens is that say user changes a field but then again he reverts back to original values but still the onchange event occurs and the flag is set although the value is back to original. how do i achieve it through javascript. thanks, dhriti.
|
 |
Eric Pascarello
author
Rancher
Joined: Nov 08, 2001
Posts: 15003
|
|
okay, basically you need to also have your code tell the difference between the onchange event fired by the user and the onchange event caused by you function. I would say you should try to set a flag in the function saying that it was called and then setting it back to false after it runs... another option you have is to diable all of the form elements after one of the items is changed. Then the user will not be able to change any of the other elements. Eric
|
 |
ChandraMouli Vidiyala
Greenhorn
Joined: Dec 19, 2002
Posts: 12
|
|
Hi dhriti, Hope this works for u. I have used 3 text boxes. Can be used with any other elements also. <html> <head> <script> var hasValue='N'; var objHasValue=''; // Holds the object, which has the data entered by user var valueEntered=''; // Holds the value entered by user function checkValue(obj){ // Loop through all the elements in the form, assign the object and the value, // which has data entered by user to the global variables. for(var i=0; i < document.myform.length;i++){ if(document.myform.elements[i].value != '') { hasValue='Y'; objHasValue = document.myform.elements[i]; valueEntered = document.myform.elements[i].value; } } // If the user has already entered value and the object got the focus is not the one // with the values, throw alert message. if((hasValue == 'Y' && valueEntered != '') && (obj != objHasValue) ){ alert("You can enter in only one of the textboxes..."); objHasValue.focus(); return; } } function assignValue(obj){ valueEntered=obj.value; } </script> </head> <body> <form name="myform"> <input type="text" name="myName" value="" onfocus="javascript:checkValue(this);" onchange="javascript:assignValue(this);"> <input type="text" name="myName1" value="" onfocus="javascript:checkValue(this);" onchange="javascript:assignValue(this);"> <input type="text" name="myName2" value="" onfocus="javascript:checkValue(this);" onchange="javascript:assignValue(this);"> </form> </body> </html> Let me know, if u face any problems. -Mouli.
|
 |
Yuriy Fuksenko
Ranch Hand
Joined: Feb 02, 2001
Posts: 411
|
|
Actually, you can use a "defaultValue" property of input element. something like: <input type=text value="initialValue" onchange="if(this.value!=this.defaultValue){setFlag(this)}else{resetFlag(this)}"> Here is more info on that property: msdn: defaultValue [ March 19, 2004: Message edited by: Yuriy Fuksenko ]
|
 |
 |
|
|
subject: problem with onchange event
|
|
|