Skip to content

Insert value to input / JavaScript

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

Answer

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!