Miuranga
Miuranga

Reputation: 2463

How to get upload file name using upload field "name" attribute in jquery

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?

Upvotes: 2

Views: 1426

Answers (2)

Jai
Jai

Reputation: 74738

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.

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82231

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

 $('[name="cover_image"].required-field').val().split('\\').pop();

Upvotes: 1

Related Questions