-1

I have this javascript function that submits a form after an onclick event. The form's name is "postdata". It works fine.

<script>
function submitform() {
    document.postdata.submit();
}
</script>

I want to change the function so I can use it for any form. I modified the function as follows:

<script>
function submitform(formname) {
    document.formname.submit();
}
</script>

It doesn't work, and I don't know why. Is my syntax wrong?

I know that the form name is being passed to the function successfully. If I temporarily change the function to this:

<script>
function submitform(formname) {
    alert(formname);
}
</script>

...the alert contains "postdata" as expected.
Thanks for your suggestions!

user3865463
  • 105
  • 1
  • 3
  • 10
  • Too bad this was marked as a duplicate. I saw the other post referred to in the "duplicate post" notice when I was originally looking for a solution to my problem. At the time I didn't recognize that it contained the answer I was looking for. Even now I barely recognize it. I won't be alone. I don't know what happens to what you mark as "duplicate posts", but I hope you don't remove them. There is a lot value is seeing things asked different ways. It improves the chances that your subscribers will come across a solution that they will recognize. – user3865463 Sep 14 '14 at 22:38

1 Answers1

3

You have to use bracket notation of accessing values.

try saying

document[formname].submit(); //this will get the value of variable "formname" and use as a key

instead of

document.formname.submit(); //this will search for a key as `formname`
Mritunjay
  • 25,338
  • 7
  • 55
  • 68