2

I have a form that has a file input control:

<input type="file" onclick="this.blur()" name="descFile" />

So when the user selects a file, the path shows in the text input box (default browser behaviour), BUT, the business would like me to put a message in bold letters below that they have selected the file to be uploaded, prior to postback.

Is there a way using Javascript to somehow capture the "select" event from file input and display in bold letters below the file input?

Mark Kadlec
  • 8,036
  • 17
  • 60
  • 96

3 Answers3

6
$('input[name="descFile"]').change(function(){
   var filename = $(this).val();
   $('<label>Filename selected: '+ filename +'</label>').insertAfter($(this));
});

Remember to put it inside or similar to:

$(function(){ });
dropson
  • 1,111
  • 2
  • 12
  • 23
2

This can be done like this

$('input[type=file]').val()

Anyway, I suggest using name or ID attribute to select your input. And with event, it should look like this:

$('input[type=file]').change(function(e){
  $in=$(this);
  $in.next().html($in.val());
});
Wazy
  • 8,822
  • 10
  • 53
  • 98
  • 1
    You should give credit to the person who wrote this originally: http://stackoverflow.com/questions/1299052/jquery-get-the-file-name-selected-from-input-type-file – Adam Fratino Mar 27 '14 at 14:34
1

It's the value attrib of your input tag, if I understand your question.

$('#file_attach').change(function(){

        path=$(this).attr('value');
        start = path.lastIndexOf(".");
        alert("extention: "+start);
});
Ivan Buttinoni
  • 4,110
  • 1
  • 24
  • 44