473,505 Members | 15,036 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy File Value to Text Value

chunk1978
224 New Member
hi there... i'd like to know if it's possible to copy a selected file's name (value?) and insert it into a basic text field thru an onchange event handler...

here is my code:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  5. <title>Untitled Document</title>
  6.  
  7. <script type="text/javascript">
  8.  
  9. function Copy()
  10.     {
  11.     document.getElementById('textfield').value = document.getElementById('filefield').value
  12. }
  13. </script>
  14.  
  15. </head>
  16.  
  17. <body>
  18. <p>
  19.   <input type="file" name="filefield" id="filefield" onchange="Copy();">
  20. </p>
  21. <p>
  22.   <input type="text" name="textfield" id="textfield">
  23. </p>
  24. </body>
  25. </html>
  26.  
Mar 1 '07 #1
12 4027
chunk1978
224 New Member
i forgot to mention: this works fine, but what i'm trying to do is just copy the file's name, instead of copying the entire address of the file...

any ideas?
Mar 1 '07 #2
acoder
16,027 Recognized Expert Moderator MVP
Your code works absolutely fine in Firefox.
Mar 1 '07 #3
chunk1978
224 New Member
Your code works absolutely fine in Firefox.
hey acoder... yeah, i forgot to add what i was really trying to do... which is to copy the file's name only instead of the entire address of the file...

perhaps there's some insane code that could read the file, and copy only the text that's after the last "/" of the file's address? but i have zero ideas...
Mar 1 '07 #4
acoder
16,027 Recognized Expert Moderator MVP
Yes, I missed your second post.

Use the string methods, in particular, lastIndexOf and substring.
Mar 1 '07 #5
chunk1978
224 New Member
Yes, I missed your second post.

Use the string methods, in particular, lastIndexOf and substring.
thanks for the advice... but i'm finding it a little difficult wrapping my head around how i could use these to do what i'd like... would i use lastIndexOf with substring? or...

???
Mar 1 '07 #6
chunk1978
224 New Member
after searching/attempting for hours, this is the best i can come up with that makes any sense to me... but it doesn't work... :-/

Expand|Select|Wrap|Line Numbers
  1. function Copy()
  2.     {
  3.     document.getElementById('textfield').value = document.getElementById('filefield').value;
  4.     document.getElementById('textfield').value.substring(lastIndexOf('/') + 1);
  5. }
  6.  
Mar 1 '07 #7
acoder
16,027 Recognized Expert Moderator MVP
after searching/attempting for hours, this is the best i can come up with that makes any sense to me... but it doesn't work... :-/
You're almost there, just one correction:
Expand|Select|Wrap|Line Numbers
  1. function Copy()
  2.     {
  3.     var fileVal = document.getElementById('filefield').value;
  4.     document.getElementById('textfield').value = fileVal.substring(fileVal.lastIndexOf('/') + 1);
  5. }
  6.  
You forgot the string object - lastIndexOf is a method of the String object.
Mar 1 '07 #8
chunk1978
224 New Member
You're almost there, just one correction:
Expand|Select|Wrap|Line Numbers
  1. function Copy()
  2.     {
  3.     var fileVal = document.getElementById('filefield').value;
  4.     document.getElementById('textfield').value = fileVal.substring(fileVal.lastIndexOf('/') + 1);
  5. }
  6.  
You forgot the string object - lastIndexOf is a method of the String object.
oh sweet, yeah ok it's makes more sense now... thanks once again acoder!
Mar 1 '07 #9
chunk1978
224 New Member
i would like to note (for future references) that while implementing this discussions' code in my script, i came across an unexpected problem...

this code's onChange event handler will not fire if the file field attached to the onChange handler loads as a default style="display:none;" ... therefore it was necessary to choose a different placement for the onChange handler...

strange but true ;-)
Mar 2 '07 #10
acoder
16,027 Recognized Expert Moderator MVP
oh sweet, yeah ok it's makes more sense now... thanks once again acoder!
You're welcome.

btw, congratulations on becoming a senior member!
Mar 2 '07 #11
acoder
16,027 Recognized Expert Moderator MVP
i would like to note (for future references) that while implementing this discussions' code in my script, i came across an unexpected problem...

this code's onChange event handler will not fire if the file field attached to the onChange handler loads as a default style="display:none;" ... therefore it was necessary to choose a different placement for the onChange handler...

strange but true ;-)
So you mean that if it was hidden by default, but then later becomes visible, when a change occurs the onchange event does not fire?
Mar 2 '07 #12
chunk1978
224 New Member
So you mean that if it was hidden by default, but then later becomes visible, when a change occurs the onchange event does not fire?
yeah, weird hey? it took me forever trying to figure out what was wrong...
Mar 2 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4310
by: tabonni | last post by:
Hello All I am creating an ASP page. There are a list of filename and checkbox next to it. When user checked all the documents they want and click ADD TO CLIPBOARD button. All filepaths will be...
4
32191
by: Risto Heinonen | last post by:
Hi. I have a web page that has images and text. I can carefully select one image and the on the right of the image and then copy & paste to Word. Is it possible to make javascript do the same:...
2
1602
by: jerrygarciuh | last post by:
Hi all, The following script is giving me weird problems. I have in this directory an index.php and hurricane.php. If the script gets $i = 'on' it is supposed to back up the current index...
17
5076
by: Steve | last post by:
I'm trying to code cut, copy, and paste in vb 2005 so that when the user clicks on a toolbar button, the cut/copy/paste will work with whatever textbox the cursor is current located in (I have...
3
2775
by: =?Utf-8?B?R3JlZyBN?= | last post by:
Hello, I'm running an asp.net, intranet web application using .net framework 1.1 on IIS5.1 / 6.0. Through the web application, I would like to press a button on the web page, have another window...
6
5437
by: Cliff72 | last post by:
I need to fill in the nulls in the batch field the value from the record immediately preceding the null one ie replace the nulls with the preceding value until I hit a record with a value in...
6
5064
by: Michael | last post by:
I need to copy a huge file (around 300Mb) from a mapped network drive to another. I have created a console application and used System.IO.File.Copy function. But I want to know the process of...
10
4942
by: Jason | last post by:
I want to create a simple program with Two buttons on the form. BUTTON 1 - BACKUP PREFS this will do the following: Copy C:\Documents and Settings\%USERNAME%\Application Data\FileZilla...
7
3827
by: raghudr | last post by:
<?xml version="1.0" standalone="yes" ?> - <AddressSpace xsi:noNamespaceSchemaLocation="prince.xsd" Name="U2" ConfigMax="1" xmlns:xsi="http://www.rag.org/2001/XMLSchema-instance"> <Item Name="raj"...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.