Auto Submit javascript

Asked

Viewed 314 times

0

I’m having a problem using the "Submit();" function on an input type="file" with an onchage passing an id to the function, I ran a test with an Alert and it’s returning me "[Object Htmlformelement]", I don’t know where the problem is.

function autoform(id) {
  alert(id);
  document.getElementById(id).submit();
}
<form action="" id="form" enctype="multipart/form-data">
  <input type="file" onchange="autoform(form);" autocomplete="off">
</form>

  • It’s all right. The variable id passed is the form, and your code is giving a alert() in the form object. What you would like to do, in fact?

  • I managed to find out where the error was, in autoform(form) missed the apostrophe, would look like this to work: autoform('form')

1 answer

0


The problem with your job is that id is not a string, rather an object, which contains the form. You could change its function to:

function autoform(form) {
   alert(form.id);
   document.getElementById(form.id).submit();
}

function autoform(form) {
   alert(form.id);
   document.getElementById(form.id).submit();
}
<form action="" id="form" enctype="multipart/form-data">
  <input type="file" onchange="autoform(form);" autocomplete="off">
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.