2

Let's say for example I have this code:

<input type="file" id="file" name="">
<input class="uploadarea">
<span class="button">Browse</span>

Now I've setupped some css to change the default input file button's look, And now the question is, How do i change .uploadarea's value depending on the value of #file when I select a file?

I currently have this code but I don't know what to do next.

var x = document.getElementsByTagName('input');
inputfile = x[0];
textbox = x[1];
6
  • 1
    What exactly do you want to do? Commented Feb 16, 2012 at 13:22
  • I want to use the filepath in the input[type=file] as the value of the input with a class uploadarea. Commented Feb 16, 2012 at 13:27
  • @user1099531 You can't get the path of a file with the file input element, just the filename. Commented Feb 16, 2012 at 13:30
  • 1
    Why are you using a class ? are there lots of these ? Commented Feb 16, 2012 at 13:31
  • ^nope, But some elements have the same styling as those ones. Commented Feb 16, 2012 at 13:36

3 Answers 3

4

Add an onchange handler to handle the change event on the file event :

<input type="file" id="file" name="" onchange="something(this.value)">
<input id="somethinghere" class="uploadarea">
<span class="button">Browse</span>

function something(val) {
    document.getElementById('somethinghere').value = val;
}

You need to add the id attribute for this to work

Sign up to request clarification or add additional context in comments.

4 Comments

Should I put this in the head or before </body>?
you can put the script in the <head> as its triggered - ie not relying on the DOM being ready
Weird. The value is undefined.
When I selected a file, the value of somethinghere is now undefined.
0

Yes. Use onchange. And by the way read alternate to onchange event in <input type='file' />. There are some problems with JS manipulation after file select.

Comments

0

Simple jQuery code:

$('#file').change(function(){
    $('.uploadarea').val(this.value);
});

pure Javascript:

<input type="file" id="file" onchange="foo()" />
<input class="uploadarea" id="other" />

function foo(){
    document.getElementById('other').value = this.value
}

2 Comments

I'm sorry but this does not include jQuery.
getElementsByClassName is not fully supported on <= IE8 -> quirksmode.org/dom/w3c_core.html#t11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.