user1096494
user1096494

Reputation:

How can I use javascript to submit a form that is in another location of the page?

Let's say I have this form in my page

<form id="myform" action="src/post.php" name="post" method="post" target="_blank">
<div id="editor"></div>
</form>

Which works flawlessly. But I need to use a button from another location in the same file to submit that form by ID. It's like a trigger command, but I believe it can only be achieved by Javascript.

Upvotes: 0

Views: 100

Answers (3)

user1096494
user1096494

Reputation:

I could make it work, but it seems that when I submit the form from another location it is not including the <div id="editor"> that is withing the form and that's why I'm getting a blank page.

Upvotes: 0

Claudio Redi
Claudio Redi

Reputation: 68400

Assuming you have a button with id submitButton, this should do the trick

<button id="submitButton">Submit</button>

You could do

$('#submitButton').on('click', function() {
    $('form').submit();
});

Upvotes: 0

zealot
zealot

Reputation: 247

Try next code

document.forms['post'].submit();

Upvotes: 1

Related Questions