tags:

views:

63

answers:

4

What is the correct way to format the document.formToSubmit.submit() line?

    var formToSubmit = 'postcomment' + id;

    alert( ''+ formToSubmit +'' );

    document.formToSubmit.submit();

The formToSubmit variable seems to be correct but the submit() does not work.

+1  A: 

The formToSubmit is now a string not an object. Try this:

 var formToSubmit = document.getElementById('postcomment' + id);
 alert( ''+ formToSubmit +'' );
 formToSubmit.submit();
Zyphrax
well that returns a: [object HTMLFormElement] but still does not submit.
ian
document.formToSubmit won't work.
Darin Dimitrov
The formToSubmit contains the actual FORM object? You can check this with alert(formToSubmit.tagName). For a good (IE focussed) reference us this: http://msdn.microsoft.com/en-us/library/ms533050(VS.85).aspx
Zyphrax
Oh and of course what darin said: don't use document.formToSubmit but just formToSubmit.submit()
Zyphrax
+2  A: 

Simply put:

document.getElementById('postcomment' + id).submit();
Sinan Taifour
nice and simple. thanks.
ian
+1  A: 

Zyphrax is on the right track. You just need to remove "document." in the last line.

var formToSubmit = document.getElementById('postcomment' + id);
alert( ''+ formToSubmit +'' );
formToSubmit.submit();

The alert is working as expected since formToSubmit is now a DOM object and the message you're receiving is the appropriate string representation. If you want to check that you have the right form you could do:

alert(formToSubmit.id);
Rojo
+1  A: 
document['postcomment'+id].submit()

should also work

knittl