2

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?

4
  • what is form? a var or the actual form tag. Commented Jun 25, 2014 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(); Commented Jun 25, 2014 at 7:45
  • 1
    This question has been answered before: stackoverflow.com/questions/6365858/… Commented Jun 25, 2014 at 7:47
  • 1
    @miuranga your form is a variable, we don't know what its value is. Commented Jun 25, 2014 at 7:49

2 Answers 2

1

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

 $('[name="cover_image"].required-field').val().split('\\').pop();
Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

You don't need to wrap form in $() as it's already a jQuery object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.