I try to trigger form submitting and prevent the submission at the same time. What I mean: When you select a file, JS will attempt to submit, but will also run a function (onsubmit) that prevents the action.
And that's my code (No jQuery, just trying to make it look like php):
var $form, $file, $files;
$form = document.querySelector('form#form-1'),
$file = $form.querySelector('input[type="file"]'),
$files = null;
$file.onchange = function($e){
$files = $e.target.files;
$form.submit();
};
$form.onsubmit = function($e){
$e.preventDefault();
console.log('Default Prevented');
// Xhr will go here, if the browser will prevent
// it from being submitted
};
I know that the problem might be because I'm confusing the browser and tells once to submit and then to prevent, but how can I make it work as expected? I'm planning to insert xhr within the $form.onsubmit function that should to the required job, but It won't, at least as long as the browser will submit the function without preventing it from being submitted.