tags:

views:

169

answers:

2

How would I submit the id into the form name to submit a certain form?

function submitComment(id)
{
    alert('comment on song id: '+[id]+'');

    document.postcomment.submit();

}

I want to be able to submit.. postcomment43 or postcomment 42.. whatever the value of ID is joint to postcomment

Tried this:

function submitComment(id)
{
    alert('comment on song id: '+[id]+'');


    var formToSubmit = 'postcomment' + id;

    alert( ''+ formToSubmit +'' );
    document.formToSubmit.submit();


}

creates the formToSubmit name correctly but doesn't fire. how do I properly place the formToSubmit variable in the document.form.submit

It seems that just putting in formToSubmit is going to look for the form with name formToSubmit

+2  A: 

Give your forms an unique ID:

<form id="postcomment42" action="..."></form>
<form id="postcomment43" action="..."></form>
<form id="postcomment44" action="..."></form>

Then use the getElementById function to get the desired form:

function submitComment(id)
{
    var formToSubmit = document.getElementById('postcomment' + id);
    if (formToSubmit != null) formToSubmit.submit();
}

Although I suspect there's something wrong with your design. Why do you have multiple forms? Can't you have a single form to submit a comment like:

<form id="postcomment" action="comment?id=42" method="post"></form>

Could you give a little more details on what the interface looks like and what you are trying to achieve?

Darin Dimitrov
Tried something. (added to edit of question)
ian
A: 

If your forms only have names, and not ID's, then you could do the following:

// by name
function submitComment(id)
{
    var theForm = 'postcomment' + id;
    document.forms[ theForm ].submit();
}

The are many, many ways to submit a form via JS, but considering your question I think this is the most applicable manner.

ken