That is to say, is it better to do this:
var firstName = document.getElementById("fname").value;
or
var firstName = document.form.fname.value;
and why?
EDIT: HTML:
input text="text" name="fname" id="fname"
That is to say, is it better to do this:
var firstName = document.getElementById("fname").value;
or
var firstName = document.form.fname.value;
and why?
EDIT: HTML:
input text="text" name="fname" id="fname"
Both work for forms, but document.getElementById also works for any other DOM element with an id, so I guess that's why it's the obvious choice today (along with newer, more flexible options such as document.querySelector).
Accessing the elements by name, however, is so ancient that it's supported even by very old browsers (such as IE4 and Netscape 2 or 3, if I recall correctly), but I doubt anyone would like/need to support those browsers nowadays.
So, there is no "better". Both work, they're just from different moments on the history of web scripting.
I prefer to have
<input text="text" name="fname" />
As this is less code and works, so I prefer to select by name.
But the important thing is you follow the same convention for all your project so you can navigate easily through your project.