Consider this HTML:
<form id="MyForm">
<input name="Input1"/>
</form>
To get the form, I would do this:
var form = document.getElementById("MyForm");
Now I know that to get Input1 from the form I can do this:
var input1 = form.Input1;
Can you give me the code for another way to access any form control from a form, by using the name attribute?
I would imagine that a possible answer could involve recursing through descendants of the form and checking for a control with the attribute named “name”, with the value “Input1”.
Please provide an answer that does not require additional libraries.
The following is not important
In case you’re wondering why I’m asking this: I’m using software called Script#, which compiles C# into JavaScript. The current version of Script# doesn’t support [form element].[name attribute value], so I’m looking for an alternative way to do this.
An alternative you might be able to use is:
Either that, or you need to assign an ID directly to the form element, and reference that. Otherwise, multiple forms may contain an element of the same name, and without including the form as part of the reference, you’d have an ambiguity.