Johnny Prescott
Johnny Prescott

Reputation: 271

Jquery submit() wait for file upload prompt

I have this simple code and I'm trying to find a way to inlcude the form submitting directly in it.

$('#falsebutton').click(function() {
    $('#fileToUpload').click();
});

How do I add $("form").submit(); so it trigger after the file upload prompt?

P.S: I don't have a submit button.

Upvotes: 1

Views: 424

Answers (1)

Vince
Vince

Reputation: 3286

The default action of a button inside a form is to submit the form. So if you want to do something else, you need to prevent the default action:

$('#falsebutton').click(function(e) {
    e.preventDefault();
    $('#fileToUpload').click();
});

Now, if I understood you right, you want to trigger the form submit immediately after the user chooses a file to upload? If so, you can listen for a change event on your file input and submit the form when it fires:

$('#fileToUpload').on('change', function() {
    $('form').submit();
});

Upvotes: 2

Related Questions