13

I am working on an AngularJS project and the attributes are numerous in many of my HTML elements:

  <button type="submit" 
          ng-click="Page.UI.DetailView.ExecuteFunction()" 
          ng-disabled="step5.$invalid" 
          class="btn btn-success pull-right">
      Submit this Product
  </button>

Is there a standardized convention for the order of these attributes? Alphabetical? Grouped by meaning (e.g. all angular attributes together)? Standard HTML attributes ("type" for example) first?

  • 6
    No, there's no standard. W3C says that the order of attributes does not matter. Find some Angular code examples somewhere on the Internet or in an Angular book, and follow whatever convention they've set there; chances are it's close to "correct," if not actually "correct." – Robert Harvey Dec 17 '15 at 16:48
  • I dont think so. But if you look at the dom in console it is alphabetically ordered. – Abel D Dec 17 '15 at 16:49
  • 1
    In addition, you can find loads of advice by Googling "Ordering attributes in HTML tags." – Robert Harvey Dec 17 '15 at 16:52
  • Thanks AbelD and @RobertHarvey. I am indeed finding plenty of opinions via Google, but was hoping that there is a more standard approach. Thanks again. – Matt Cashatt Dec 17 '15 at 16:58
  • 1
    @AbelD Alphabetical ordering of HTML attributes sounds like the worst idea ever though, for a developer. – Alternatex Dec 18 '15 at 13:15
13

There is no standard. Optimize for ease of understanding, and try to be consistent.

Personally I think it's also good to always put the id attribute first, so that it's easier for testers to find it.

  • 3
    I find the most important thing is to be concise. If working in a team, make sure you follow the same pattern for the aforementioned reason. I tend to stick to type, id/for, name, class. If I'm using angular then all ng--attributes will go first. It's down to personal preference. – die maus Dec 17 '15 at 17:08

protected by gnat Mar 16 '18 at 12:20

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.