HTML Form: Do we need a method & action attribute in the form element for Username & Password?

Question: Do we need a method & action attribute in the form element for Username & Password?

When we do a normal text-based form, we add the method & action attribute in the form element.
But when doing the username & password input, do we not add these attributes?

Web Development course: HTML Forms
https://www.codecademy.com/paths/web-development/tracks/learn-html-web-dev-path/modules/learn-html-forms/lessons/html-forms/exercises/password-input

Example code for a text-based input:

  <form method="post" action="http://server1">
		<h4>Text based Form input:</h5>
		Enter your name:
		<input type="text" name="fname">
		<br/>
		Enter your age:
		<input type="text" name="age">
		<br/>
		<input type="submit" name="Submit">
	</form>

An example of a password input:

            <form>
		<label for="user-password">Password: </label>
		<input type="password" id="user-password" name="user-password">
	</form>

Short answer: No, method and action attributes are applied to the form itself, not its elements (including inputs).


The method attribute passed to elements of type form determines how the results of that form will be sent. More information here.

The action attribute determines where the results of that form will be sent. More information here.


So the reason you do not need to specify a method or action value for each input element is twofold:

  1. They actually don’t exist. According to the MDN web docs, input elements do not have either of those attributes.
  2. There’s no reason to. Even if input elements had the necessary attributes, the form itself is responsible for determining where all of the information it receives is sent on submission. Requiring you to specify that same information for each element would be, at best, superfluous, and at worst, maliciously designed to make you hate using forms (…only joking :stuck_out_tongue_winking_eye:).

If and when you start working with the information that is actually input into these forms, these attributes will probably start to make more sense in practice, but for now, I’ll leave you with the same TL;DR I started the post with: No, it’s not required.