0
Formerly I called a specific form per creation ID in the body of the document:
document.forms[0]
Only then did I understand what the attribute is for NAME
in these cases, but I was in doubt, for example, I have more than one form on the page, can be a login or registration and other search, so I assign the corresponding name for 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>
Only I don’t even know the right way to do it, both work in Firefox, Chrome and Opera:
1°:
document.forms['login']
2°:
document.forms.login
So I usually do like this:
var form = document.forms['login'] || document.forms.login
And I don’t even know if this really works, but I do it as a precaution in case some browser doesn’t work.
So what would be the best or most correct way to work in any browser on any platform?
Also worth remembering
document.querySelectorAll("form")[0]
.– NoobSaibot