Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I get upload file name after click the submit button. Below in the file upload field.

<input class="required-field" type="file" value="" name="cover_image">

I've tried with below but it not get the value.

var filename = $("[name='cover_image']", form).val();

Here I don't have ID fro the input filed but is it possible to get upload file name?

share|improve this question
    
what is form? a var or the actual form tag. –  Jai Jun 25 at 7:41
    
I use form for get the value in filed. ex: for get text field value I've used var magcode = jQuery("[name='magcode']", form).val(); –  miuranga Jun 25 at 7:45
1  
This question has been answered before: stackoverflow.com/questions/6365858/… –  Khalid T. Jun 25 at 7:47
1  
@miuranga your form is a variable, we don't know what its value is. –  billyonecan Jun 25 at 7:49
add comment

2 Answers

up vote 1 down vote accepted

You will get the full path using .val() for getting filename you can use:

 $('[name="cover_image"].required-field').val().split('\\').pop();
share|improve this answer
add comment

Try this instead:

var filename = $("[name='cover_image']", 'form').val();

or one suggestion is to use form's id here:

var filename = $("[name='cover_image']", "formID").val();

or you can try this too:

var form = $('YourForm');
var filename = form.find("[name='cover_image']").val();

you can use form because this is the jQuery object and you can find the specific element in it.

share|improve this answer
1  
You don't need to wrap form in $() as it's already a jQuery object. –  Matt Ellen Jun 25 at 7:45
    
yeah! thats right but for a readable sake i just did it. –  Jai Jun 25 at 7:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.