0

When user clicks submit on a form, I want to modify the contents of one field and then submit. I've now tried about 20 different ways to do this, but none of them work. I'm able to recognize the submit event, cancel it, and modify the fields, but I'm unable to submit the form again in the same event. My latest attempt was with JQuery:

    $("form").submit(function (e) {
        e.preventDefault();
        var element = document.getElementById("categorySafari");
        var selection = element.options[element.selectedIndex].text;
        document.getElementById("category").value = selection;
        this.submit(); // <-- this doesn't work and nothing I've tried here works
    });
5
  • Have you checked for errors in the console? Commented Jan 8, 2017 at 15:26
  • "Submit is not a function" Commented Jan 8, 2017 at 15:28
  • of course you can't submit, you cancel each submit! heh... infinite loop? use a flag of some sort to determine if the cleanup has run on not, and don't preventDefault() if it's clean. Commented Jan 8, 2017 at 15:31
  • I think that you enter an infinite recursion because you always cancel the default behavior. You should change some attribute of the submit event in order to not cancel the default after the first reentry Commented Jan 8, 2017 at 15:38
  • There was no infinite loop. I got infinite loop earlier by using $("form").submit(); at the last line. This way it's just unable to call the submit function at all. Commented Jan 8, 2017 at 15:40

3 Answers 3

1

Remove the e.preventDefault() and the this.submit() call. That is preventing the form submission.

Sign up to request clarification or add additional context in comments.

1 Comment

And also remove this.submit(), since the form will be submitted automatically.
0

Maybe try to put onClick event to your submit button and call function to handle your fields then from this function call submit form event.
Here is how you can trigger form with js

Comments

0

I've tried your code and it works correctly for me (Ubuntu+Chromium): http://codepen.io/swergas/pen/qROWbY

Values Posted

categorySafari  volvo
category    Volvo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.