Leem.fin
Leem.fin

Reputation: 42602

How do I trigger Form Submission in Javascript?

I have initialized my form submission like following:

$(document).ready(function() {
   $("#my_form").submit(function(e) {
         ...
         ...
   }
}

As you see above, it is in $(document).ready(...). When user press "Submit" button on UI, the form will be submitted.

But, How can I also trigger this form submission in Javascript besides user input (e.g. press submit button on UI)?

Upvotes: 1

Views: 2760

Answers (3)

user1006544
user1006544

Reputation: 1524

 $(document).ready(function () {
        $("#SubmitForm").click(function (e) {
            var textContent = $("#TextContent").val();
            textContent = jQuery.trim(textContent);
            if (textContent == "") {
                alert("Content field cannot be empty.");
                $("#TextContent").focus();
                return false;
            }
            else{ $("#my_form").submit();
            }
        });
    });

Upvotes: 1

techfoobar
techfoobar

Reputation: 66663

You can use $("#my_form").submit();

Upvotes: 1

Treffynnon
Treffynnon

Reputation: 21553

Call the submit() DOCs method on the form.

$("#my_form").submit();

Upvotes: 3

Related Questions