PDA

View Full Version : how to refresh a field and not all form?



hantz
09-29-2007, 12:22 AM
<form name="formData">
<input type="file" name="FILE1" onchange="setdumy(value)">
</form>
i got this form for example...its name "formdata"

when i want to clean=reset all the form i will use

formData.reset();

what is the command to reset the specific field of of the file called FILE1
?:rolleyes:

djr33
09-29-2007, 12:39 AM
Reset to nothing? Something like--
generalproperty.file1.value=''; this.value='';

jscheuer1
09-29-2007, 04:59 AM
If it were a text input:


document.forms['formData']['FILE1'].value='';

However, you are not (at least in most modern browsers) allowed for security reasons to mess with the value of a file input. If you need to reset just one file input at a time, use multiple forms, one for each file input, and reset it's particular form.

djr33
09-29-2007, 05:41 AM
I suppose you could kill that then rewrite it.
div.innerHTML = '<new file input>';

jscheuer1
09-29-2007, 06:38 AM
I suppose you could kill that then rewrite it.
div.innerHTML = '<new file input>';

This might be one instance where the habit of innerHTML to obliterate form data might be useful, provided browsers will allow it and execute it as desired.

hantz
09-29-2007, 01:51 PM
for example lets take :

<form method=post name=upform action="/cgi-bin/some-script.cgi" enctype="multipart/form-data">
<input type=file name=uploadfile1 onchange="LimitAttach(this.form, this.form.uploadfile1.value)">
<input type=file name=uploadfile2 onchange="LimitAttach(this.form, this.form.uploadfile2.value)">
<input type=file name=uploadfile3 onchange="LimitAttach(this.form, this.form.uploadfile3.value)">
<input type=button name="Submit" value="Submit">
</form>

calls function


function LimitAttach(form, file) {
???????
}


what i have to write there in order to reset the field of the choosen file?

document.formData.value='';
generalproperty.file1.value=''; this.value='';
document.getElementById(FILE).value='';

:confused:

jscheuer1
09-29-2007, 03:34 PM
First of all, there isn't much point passing so much data to LimitAttach(). This will suffice:


<input type=file name=uploadfile1 onchange="LimitAttach(this)">

You can get all that other stuff you may need once LimitAttach() has the input element as represented by its this keyword:


function LimitAttach(el) {
var thevalue=el.value;
/*do whatever you like with thevalue here*/
//now we can remove and replace the input:
var newinput=document.createElement('input');
newinput.name=el.name;
newinput.type='file';
newinput.onchange=function(){LimitAttach(this);};
el.form.replaceChild(newinput,el);
}

Notes: I tested this in Opera, FF, and IE. In Opera, thevalue only contained the filename, not the path. In the others, thevalue contained the full path and filename. In all three, the variable thevalue retained its value even after the input was replaced. There could be memory leaks in IE. You might want to set el to null or something like that after retrieving its value and replacing it.

hantz
09-29-2007, 05:45 PM
i psot this msg without all the script..its realy long and boring and not relevant...the 2 data i send is for this script...
wrote onchange="LimitAttach(this.form, this.form.uploadfile1.value)
if i will use you script i will need to add third data
onchange="LimitAttach(this.form, this.form.uploadfile1.value, this)

about the script ittself it is very interesting realy i have never thought about this direction...but all i wanted when i said reset\clean is realy to clean...that it will be empty...

thank you again for you help:)

jscheuer1
09-29-2007, 06:08 PM
In my version of LimitAttach() called with:


<input type=file name=uploadfile1 onchange="LimitAttach(this)">

this.form would be represented by el.form. this.form.uploadfile1.value would be represented by el.value.

Try this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function LimitAttach(el) {
var thevalue=el.value;
/*do whatever you like with thevalue here*/
alert(thevalue);
alert(el.form.innerHTML);
//now we can remove and replace the input:
var newinput=document.createElement('input');
newinput.name=el.name;
newinput.type='file';
newinput.onchange=function(){LimitAttach(this);};
el.form.replaceChild(newinput,el);
}
</script>
</head>
<body>
<form method=post name=upform action="/cgi-bin/some-script.cgi" enctype="multipart/form-data">
<input type=file name=uploadfile1 onchange="LimitAttach(this)"><br>
<input type=file name=uploadfile2 onchange="LimitAttach(this)"><br>
<input type=file name=uploadfile3 onchange="LimitAttach(this)"><br>
<input type=button name="Submit" value="Submit">
</form>
</body>
</html>

You will see that the input, its value, and its form are all available from its this keyword.