Reputation: 8101
I am getting Uncaught TypeError: Illegal invocation in Chrome or SCRIPT65535: Invalid calling object in IE when submit form.
Following markup reproduces error:
<!DOCTYPE html>
<html>
<body>
<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>
<form id="myForm" action="form_action.asp">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br><br>
<input type="button" onclick="myFunction()" value="Submit form">
</form>
<script>
function myFunction()
{
var submit2 = myForm.submit;
submit2();
}
</script>
</body>
</html>
But when instead lines:
var submit2 = myForm.submit;
submit2();
Directly sumbit form myForm.submit() then all work without error.
So how to fix problem is clear for me, for me is interesting why "indirect" form submit causes errors
Upvotes: 3
Views: 1128
Reputation: 1082
The submit method when called with myForm.submit()
knows the context is myForm, but when we are calling it using submit2()
, there is not context ( global context), so its giving error.
To fix the error, you have to call the submit2()
method by setting the content of this to the myForm.
The sample code is
var submit2 = myForm.submit;
submit2.call(myForm);
You can either use call or apply for changing the context.
More Info on call and apply:
Upvotes: 3