Results 1 to 3 of 3

Thread: Resetting A Single Field Among Others

  1. #1
    Join Date
    Mar 2007
    Posts
    51
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Resetting A Single Field Among Others

    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.

    <tr>
    <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>
    </tr>
    Last edited by TimeTracker; 06-16-2008 at 07:54 AM. Reason: corrected resetField

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Your code seems to be incorrect. Have a look at the following code that will do what you are looking for

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    	<head>
    		<title>Demo Page</title>
            <style type="text/css">
    
    		</style>
    		<script type="text/javascript">
    		function resetField(name,value){
    			document.forms['form'].elements[name].focus();	
    			document.forms['form'].elements[name].value = value;		
    		}
    		</script>
    	</head>
    	<body>
    		<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');" />
    		</form>
    	</body>
    </html>
    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.

  3. #3
    Join Date
    Mar 2007
    Posts
    51
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Resetting A Single Field Among Others

    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!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •