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

Calling all Javascript gurus

Status
Not open for further replies.

surzycki

IS-IT--Management
Feb 9, 2002
62
0
0
FR
Hello,

I am trying to build a special upload component in javascript. The problem that I am having is that I would like to clear out the file input tag on only certain file input fields (not all). As well, I don't want to have to reload the page. Imaging a situation in which a user has selected 5 files and wishs to remove one of them before uploading. I would like the user to be-able to click a button and the file input field would be zeroed out (no page reload is VERY important) Is there a way to do this that is cross browser compatible?

Stefan
 

You may well be able to do this by removing the file upload control using the DOM, and then re-writing it - again, using the DOM. Something like this works for me in both IE6 and FF1:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function clearFileBox(whichBox) {
			var container = document.getElementById(whichBox + 'Container');
			container.removeChild(container.firstChild);
			var newFile = document.createElement('input');
			newFile.type = 'file';
			newFile.name = whichBox;
			container.appendChild(newFile);
		}
	//-->
	</script>
</head>
<body>
	<form>
		<div id="file01Container"><input type="file" name="file01" /></div>
		<input type="button" value="'Clear' File Input" onclick="clearFileBox('file01');" />
	</form>
</body>
</html>

I cannot guarantee the naming scheme of the file boxes submitted to the server - so you will need to confirm this.

Hope this helps,
Dan
 
I've done this before using slightly different code. It may not be as cross-browser friendly as Dan's example, but it's handy if you've made some run-time style changes to the box and you want to keep those changes.
Code:
<html>
<head>
    <script type="text/javascript">
    <!--
    function clearFileBox(whichBox) {
      var myFile=document.getElementById(whichBox);
      myFile.outerHTML=myFile.outerHTML;
    }
    //-->
    </script>
</head>
<body>
    <form>
        <input type="file" name="file01" />
        <input type="button" value="'Clear' File Input" onclick="clearFileBox('file01');" />
    </form>
</body>
</html>

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top