Forum Moderators: open
Does anyone know how to send just the file name value itself to a new text input box:
when someone selects a file via the browse button the field will display something like:
c:\thedirectory\sub\Test.Gif
I would like to pass this information via an onclick command to another Form Field (Normal input NOT file) but just the name itself:
Test.Gif
Any Ideas?
Kind Regards
Walker
If MyString contained "c:\thedirectory\sub\Test.Gif", then
MyString.lastIndexOf('\');would return 19, the position of the last slash.
MyString.lengthwould return 28. You can then use
MyString.substring[m, n]to extract Test.Gif.
An alternative method is to use MyString.split('\'), which would return an array containing "c:", "thedirectory", etc
Beware that you should check for '/' as well, and also handle the case where the '/' or '\' is not found.
Shawn
I'm not sure what your level of knowledge is, so this is difficult to answer... If you know nothing about javascript, then you should search the web and also search this site (see the link at the top of the page) for "javascript tutorial". Below are a couple of tips just to get you started...
In your html file, you can define functions like so:
<script language="javascript">
function sayHello() {
alert('hello world');
}
</script>
or you can put
function sayHello() {in a file called filename.js, and then put the following in the html file:
alert('hello world');
}
<script type="text/javascript" language="javascript" src="filename.js"></script>
Then your onclick tag attribute can be set to
onclick="sayHello()"
To get to the form fields, you need to name the form and name the fields. Then in your function, you can do:
MyString = document.forms['FormName'].elements['x'].value;
If you know javascript and I am misunderstanding your question, please clarify.
Shawn
I know enough about JS to get by. What I meant was how do I impliment the lines of code provided to do the job I mentioned at the begining.
I have worked this out now myself the simplified version of the code is as follows:
<form name="TestForm">
<input type="file" name="TheFile">
<input type="text" name="NameOnly">
</form>
<SCRIPT>
function ExtractName(){
var Data = document.TestForm.TheFile.value;
var a = Data.lastIndexOf('.');
var b = Data.Length
var FileNameOnly = Data.substring(a+1, b)
TestForm.NameOnly.value=FileNameOnly
}
</SCRIPT>
<a onclick="ExtractName()">SUBMIT</a>
The Problem is that lastindexOf works with any char other than "\" (is currently set to .) why is this and is there a way to make it work?
Kind Regards
Walker