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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clearing an Input Text

Status
Not open for further replies.

SDyke

MIS
Oct 13, 2005
55
US
I want the user to be able to click a button an clear the contents of an input box. The input box os of the type file and does not respond to the document.getElementById('myBox').value = "";
 
SDyke, unfortunately I have bad news for you. You can not dynamically change the value of an input of type file. This is because of security reasons. Imagine you are uploading an image to a server using an input of type file. And, right as you hit the submit button, javascript changes the input to point at a different file instead. You could be uploading potentially sensitive information to a webserver and not even know that it's happening...

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
As kaht said, you cannot change the value in the file input.
But I think you could clear the value with a reset method on the form or a reset button.
I haven't tried it myself but you can try it..

Code:
<form action="a_page.php" method="GET">
<input type="file" /><br />
<input type="reset" value="Clear form" />
</form>


- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Yes, however keep in mind that this will (obviously) reset all fields in your form, which is something that you may consider undesirable.

 
You could always do a quick round-up of all the current form values, plop them in an array, reset the form and repopulate the values from the array.

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
Yes I know. Or you could replace the input field with a new one. If you have many fields in the form this might be a good solution.

- Lowet

[gray]Why can't all browsers parse pages the same way? It should be the Web designer who decides how to display the content, not the browser![/gray]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top