-3

I need to press the button "submit" if the upload file format is valid. I have managed to check the format using the JavaScript below. My aim is between // and // ...

<script>
function Checkfiles(f){
    f = f.elements;
    if(/.*\.(gif)|(jpeg)|(jpg)|(doc)$/.test(f['filename'].value.toLowerCase()))
        return true; + // and press the button submit //
    alert('Please Upload Gif or Jpg Images, or Doc Files Only.');
    f['filename'].focus();
    return false;
};
</script>

And here's the HTML form:

<form action="something.php" method="post" name="myForm" onsubmit="return Checkfiles(this);">  
<input type="file" name="filename" accept="/image*/">
<input type="submit" name="submit"> 
</form>

Thanks.

Boann
  • 48,794
  • 16
  • 117
  • 146
  • 2
    Given that your function is called from the form's `onsubmit`, what effect do you think programmatically pressing the submit button will have? Please describe what you are trying to achieve from the user's point of view. – nnnnnn Nov 22 '14 at 14:14
  • 1
    Returning `true` submits the form, why would you need to trigger a click on the submit button inside that function ? – adeneo Nov 22 '14 at 14:14
  • Java is not Javascript. – khelwood Nov 22 '14 at 14:14

1 Answers1

0

Do something like:

document.querySelector('form[name="myForm"] input[type="submit"]').click();
Gonsalo Sousa
  • 495
  • 4
  • 9
  • 1
    Why dont you trigger the form's submit event instead? – sabithpocker Nov 22 '14 at 14:32
  • You'r right! But I'm just giving an answer to the question,`I need to press the button "submit"`, and the submit event is being called already, so don't know why he needs to trigger a click anyway. – Gonsalo Sousa Nov 22 '14 at 14:41
  • What do you mean by "In this case the $ isn't jQuery, but the DOMElement"? – nnnnnn Nov 22 '14 at 15:06
  • @nnnnnn The `$` is kind of a shortcut to `document.querySelector`. [Read this.](http://stackoverflow.com/a/22244912/4267501) – Gonsalo Sousa Nov 22 '14 at 15:49
  • That shortcut is defined by certain browsers _in the console only_ - it is not available to code running in the current page. (And in the case of the OP's code presumably `$ === jQuery` given that they tagged the question with "jquery".) – nnnnnn Nov 23 '14 at 01:03
  • By using the `document.querySelector` you obtain the same expected result – Gonsalo Sousa Nov 23 '14 at 11:20