View Full Version : Resetting A Single Field Among Others

06-16-2008, 07:53 AM
Is there a way to reset an individual text field's value in a form of several fields to 0.0 (or any other value) without resetting all the fields? A small button (e.g. value="0.0") pressed would do the job nicely.
I have searched the web looking for an answer. input type=reset clears all fields. I only want to reset a specific field without affecting the others and at the same time allowing the main function to work normally. Isolating the field with <form> and </form> stops the main function from working. If possible, I can find good use for the code. Here is one unsuccessful attempt at it.

<td><input type="text" name="adjustment" size="10" value="0.0"><input type=button value="0.0" onClick="javascript:this.form.adjustment.focus();this.form.adjustment.resetField();"></td>

06-16-2008, 08:58 AM
Your code seems to be incorrect. Have a look at the following code that will do what you are looking for

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<title>Demo Page</title>
<style type="text/css">

<script type="text/javascript">
function resetField(name,value){
document.forms['form'].elements[name].value = value;
<form name="form">
<input type="text" name="adjustment1" size="10" value="10.0" />
<input type="text" name="adjustment2" size="10" value="15.0" />
<input type="text" name="adjustment3" size="10" value="12.0" />
<input type=button value="reset field" onclick="resetField('adjustment2','0.0');" />

In the above code you can see that there is a JavaScript function named 'resetField' that accepts two parameters:
(1) The name of the form field which you want to change.
(2) The new value for the to be changed element.

In the button's onclick event we call the function and pass 'adjustment2' in other words we need to replace whatever value inside the 'adjustment2' form field with '0.0'. Please note that if you pass 0.0 without using any quotes then it will be consdered as numeric type and when the value comes in the form field the decimal point section will be removed. So I've treated it as a string for the time being.

You've mentioned that you've tried to use 'reset' field for this job and it clear all the form fields. Actually the functionality of this is resetting the form element, which is not suitable for your job as you've mentioned.

You can also extend the script in such a manner that you can reset a number of fields to a single value or to different values also.

Please let me know if you need any more help on this thing.

06-16-2008, 11:32 AM
codeexploiter! Thanks. It finally worked after modifying the form name ('moduleForm') and only formatting input text boxes. Your code is invaluable, especially where two choices are given and both have "0.0" default values and there is a need to swap from one to the other (I have four 0.0s on one form). I hope that someone else searching for this type of code can find it easily. In eight years of receiving JavaScript newsletters, not once have I come across this. Again, Dynamic Drive has proven itself to be the best! Cheers!