javascript - get file name from file upload using jquery and set filename to a text field -


i have filename of uploaded file , set textfield

i have done follows

<input type="file" name = "filename" id="upload"> <input type="text" name = "file" id="file"> 

//jquery

$('#upload').change(function() {     var filename = $('input[type=file]').val().split('\\').pop();     var lastindex = filename.lastindexof("\\");     $('#file').val(filename); }); 

and tried this

$('#upload').change(function() {     var filename = $(this).val();     var lastindex = filename.lastindexof("\\");     if (lastindex >= 0) {         filename = filename.substring(lastindex + 1);     }     $('#file').val(filename); }); 

but filename not displayed in textfield.

you supposed using jquery inside document ready. might have forgotten add that. here working code.

      $(document).ready(function() {        $('#upload').change(function() {         var filename = $('input[type=file]').val().split('\\').pop();        console.log(filename,$('#file'));          var lastindex = filename.lastindexof("\\");             $('#file').val(filename);      });        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>  <input type="file" name = "filename" id="upload">                                  <input type="text" name = "file" id="file">


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -