2

I want to use the underscore "_" symbol as a key name to my framework. Is there anything wrong with setting the name of anattribute of a tag to an underscore? for example

<div class="dropdown sandstone-texture" _="choices"></div>

For that matter. what about "$"

  • 4
    Yes, it is not good as per the spec. Use `data-*` attributes. I believe, you can use `data-_="choices"`. – Praveen Kumar Purushothaman Mar 10 '16 at 23:12
  • 3
    I think a question to ask yourself is "Why would I want to do this?" To quote Dr Malcolm: "[...] preoccupied with whether or not they could that they didn't stop to think if they should." – Drew Kennedy Mar 10 '16 at 23:14
  • @DrewKennedy **Legibility**. Development speed. PraveenKumar Google's Angular.js using ng-* so why can they do custom attributes but not anyone else? – Noah Watchmaker Mar 10 '16 at 23:27
  • `_` is not a syntax error, but `div` elements have no `_` attribute, so it's not allowed. It would be the same for `hello`. If you want custom attributes, use `data-*`. – Oriol Mar 10 '16 at 23:30

1 Answers1

0

In short, _ as the entire attribute's name, is considered wrong because since it is the only character it is also the first character, and as long as you are calling this an "HTML" attribute, there is indeed an HTML rule against attribute names starting with _.

Explanation

From 20 SGML Declaration of HTML 4:

NAMING   LCNMSTRT ""
         UCNMSTRT ""
         LCNMCHAR ".-_:"    
         UCNMCHAR ".-_:"
         NAMECASE GENERAL YES
                  ENTITY  NO

SGML and HTML Explained:

By default SGML presumes that names can only start with alphabetic characters, in either shift, with subsequent characters being alphanumeric. The LCNMSTRT and UCNMSTRT entries in the syntax clause allow other, non-alphanumeric, characters to be defined as name start characters, the LCNMCHAR and UCNMCHAR entries defining which non-alphanumeric characters can be used as name characters after a name start character.

So it we see _ is listed in LCNMCHAR, UCNMCHAR, and that they are to be after a name start, not before.

Further Reading

Which ASCII characters are forbidden for use in SGML attributes?

clarity123
  • 1,956
  • 10
  • 16