lingo
lingo

Reputation: 1908

Function to submit form by id

How can I submit form using form id as function param? Would it be something like that:

HTML:

<a href="#" onclick="confirmSubmit(form1)">Delete file</a>

Javascript:

function confirmSubmit(id) {
    var agree=confirm("Are you sure you wish to delete this file?");
    if (agree)
        document.getElementById('form' + id).submit();
    else
        return false;
}

Upvotes: 1

Views: 469

Answers (2)

RevanthKrishnaKumar V.
RevanthKrishnaKumar V.

Reputation: 1903

Define an id to form (if you want to refer by ID)

<form id="form">

</form>

<a href="#" onclick="confirmSubmit('form')">Delete file</a>

function confirmSubmit(id) {
    var agree=confirm("Are you sure you wish to delete this file?");
    if (agree)
        document.getElementById(id).submit();
    else
        return false;
}

Upvotes: 0

Aravinder
Aravinder

Reputation: 503

You can pass form id as string parameter(means with in single quotes) to the onClick function.

see below

<a href="#" onclick="confirmSubmit('form1')">Delete file</a>
function confirmSubmit(id) {
    var agree=confirm("Are you sure you wish to delete this file?");
    if (agree)
      document.getElementById(id).submit();
    else
     return false;
}

Upvotes: 1

Related Questions