I used to call a specific form by creation ID in the body of the document:
document.forms[0]
Only after I was able to understand what the NAME
attribute is in these cases, but I was in doubt, for example, I have more than one form on the page, it can be a login or registration and another search, so I assign the corresponding name to each one, and I want to use the login form to validate by Javascript:
<form name="login">
<input type="text" value="" name="user" required />
<input type="password" value="" name="user" required />
<input type="button" onclick="validateLogin()" />
</form>
But I do not even know the right way to do this, both work in Firefox, Chrome and Opera:
1 °:
document.forms['login']
2 °:
document.forms.login
So I usually do this:
var form = document.forms['login'] || document.forms.login
And I do not even know if this works, but I'll be careful if any browser does not work.
So, what would be the best or most correct way to work on any browser on any platform?