3

I'm just curious about a certain scenario:

If I want to submit a form using getElementById using the following code below:

 document.getElementById("form_id").submit();

it works fine. However, trying similar code using getElementsByName in the code below:

document.getElementsByName("form_name").submit();

doesn't work, although there is only a single element with this name: form_name.

So my question is?

Is it possible to submit a form using getElementsByName, or do I need to give an id to all of my forms.

Thanks!

James Drinkard
  • 15,342
  • 16
  • 114
  • 137
frequent
  • 27,643
  • 59
  • 181
  • 333

1 Answers1

10

document.getElementsByName returns an array, so you need to access it with the array index notation:

document.getElementsByName("form_name")[0].submit();
chrisfrancis27
  • 4,516
  • 1
  • 24
  • 32