Connecting Tech Pros Worldwide Forums | Help | Site Map

Copy File Value to Text Value

chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#1: Mar 1 '07
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.  

chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#2: Mar 1 '07

re: Copy File Value to Text Value


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?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Mar 1 '07

re: Copy File Value to Text Value


Your code works absolutely fine in Firefox.
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#4: Mar 1 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by acoder

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...
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Mar 1 '07

re: Copy File Value to Text Value


Yes, I missed your second post.

Use the string methods, in particular, lastIndexOf and substring.
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#6: Mar 1 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by acoder

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...

???
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#7: Mar 1 '07

re: Copy File Value to Text Value


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.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Mar 1 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by chunk1978

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.
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#9: Mar 1 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by acoder

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!
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#10: Mar 2 '07

re: Copy File Value to Text Value


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 ;-)
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#11: Mar 2 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by chunk1978

oh sweet, yeah ok it's makes more sense now... thanks once again acoder!

You're welcome.

btw, congratulations on becoming a senior member!
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#12: Mar 2 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by chunk1978

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?
chunk1978's Avatar
Familiar Sight
 
Join Date: Jan 2007
Posts: 226
#13: Mar 2 '07

re: Copy File Value to Text Value


Quote:

Originally Posted by acoder

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...
Reply