I have the following form:
<form id="vintro-upload-form" action="{url}?nexturl={nextUrl}" method="post" enctype="multipart/form-data" >
<input name="file" type="file"/>
<input name="token" type="hidden" value=""/>
<input value="Upload Video File" type="button" name="submit" onClick="checkFile()" class="button" />
</form>
and the following javascript:
<script>
function checkFile(){
var fileVal = document.forms["vintro-upload-form"].elements['file'].value;
//RegEx for valid file name and extensions.
if(fileVal != ""){vintro-upload-form.submit();}
else {alert('Please select the Video file.');}
}
</script>
What works? The fileVal assignment is good. Submit isn’t working, when I checked the debugger, it is saying: “vintro is undefined.”
What have I tried?
The following examples and code:
- http://www.w3schools.com/jsref/met_form_submit.asp
- http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
- document.vintro-upload-form.submit();
- document.forms[“vintro-upload-form”].submit();
- document.getElementById(“vintro-upload-form”).submit();
- changed the dashes/hyphens to underscores
All to no success. There is some jQuery (in another .js file) going on with this form as well, and it functions properly with the hyphenated name.
Why is the submit call not working?
EDIT:
I have tried using document.getElementById('vintro-upload-form').submit(); as suggested in the answers to this question, however, this is still failing. My (Firefox) debugger is saying that it is not a function. Chrome’s debugger explained a little bit more: “Uncaught TypeError: Property ‘submit’ of object # is not a function” (I’m going to Google this in the meantime.)
Since I have less than 100 reputation:
Ok, so it turns out, I was asking the wrong question.
Here’s the answer I found:
Submitting Form Via Javascript, form defined in external PHP file
Check the accepted answer as well.
This JavaScript:
document.forms["vintro-upload-form"].elements['file'].valueis looking for the NAME attribute of the form tag:
document.forms[{form_name}].elements[{field_name}].valueYou just have an ID. Copy the value of the ID to a NAME and you’re done.
However, document.getElementById() is the preferred, modern way of doing this with plain JavaScript.
HOWEVER: You can’t have a submit / button named “submit”.
When you do and you call the JavaScript submit() function, you get a conflict.