2

I have the following JS / HTML code:

<input type="text" class="file" name="file_info" id="file_info">
    <div class="file_upload">
        <input type="file" id="file_upload" onchange="name();">
    </div>
<script>
    function name() {
        var fileName = document.getElementById("file_upload").value;
        var fnSplit = fileName.split(/[\/\\]/);
        fileName = fnSplit[fnSplit.length - 1];
      document.getElementById('file_info').innerHTML = 'Fred Flinstone';
    }
</script>

I want that after I upload file, the file-name will shown in the tput text, but this cide doesn't work.

How can I fix it?

Update : The file name should be inside the input text

1
  • 2
    I don't think you are supposed to use innerHTML on input elements Commented Jul 13, 2013 at 15:00

3 Answers 3

3

Move your script element before the input element. You had better put the script element inside your head like this

Demo

Have update the answer!

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

5 Comments

Input elements don't use innerHTML either. Probably want value.
The file name should be in sepreat input text
@Jonathan we can get the file path form file object element w3schools.com/jsref/dom_obj_fileupload.asp
@BradChristie sorry, thanks for your opinion, I have checked the code and fixed it.
@NaveTseva You can check it now, I just forget update the link, the current revision is 3.
1

You can try this code:

<!DOCTYPE html>
<html>
    <head>
        <script>
            function myFunction()
            {
                var fileName =document.getElementById("file_upload").value;
                var fnSplit = fileName.split(/[\/\\]/);
                fileName = fnSplit[fnSplit.length - 1];

                document.getElementById('file_info').value = fileName
            }
        </script>
    </head>
    <body>

        <input type="text" class="file" name="file_info" id="file_info">
        <div class="file_upload">
            <input type="file" id="file_upload" onchange="myFunction();">
        </div>
    </body>
</html>

Comments

-2

Do it like this:

HTML

<div class="file_upload">
    <input type="file" id="file_upload">
</div>
<div id="file_info"></div>

JS

function name(file) {
    var reader = new FileReader();
    reader.readAsText(file);

    reader.onload = function (event) {
        document.getElementById('file_info').innerHTML = "Filename: " + file.name + "\nContent: " + event.target.result;
    };

    reader.onerror = function () {
        alert('Unable to read ' + file.name);
    };
}

document.getElementById('file_upload').onchange = function () {
    name(this.files[0]);
};

DEMO

1 Comment

The file name should be in sepreat input

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.