5

I have just noticed that I am coding stuff like this:

<input type="text" ng-model="userName" id="userName" name="userName" />

And it occurs to me that I have access to the data though ng-data. I will never use getElementById() or getElementsByName().

Are theid= and name=just cluttering my HTML, or might I ever need them?


[Update] might they be of some use for automated testing?

3
  • 2
    If you ever need them, add them when you need them. Commented Apr 4, 2016 at 9:36
  • 2
    name might be useful for accessibility and automation. For example for a password manager. Commented Apr 4, 2016 at 9:41
  • Can you elaborate on that? Feel free to post an answer. Commented Apr 4, 2016 at 9:53

1 Answer 1

1

Usually, the name attribute in input tags is used for form validation in the HTML scope. The errors for each element would be available in the form of: myFormName.MyElementName.$error.

For example, if your input tag has the 'required' attribute, the error flag would be accessible with myForm.myElement.$error.required

Here is an example that requires the name attribute (validate some field in a directive).

1
  • 2
    And id can be useful for animation, jquery, etc. But no reason to include it if not used. Commented Apr 5, 2016 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.