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
  • 4
    Yes, it is not good as per the spec. Use data-* attributes. I believe, you can use data-_="choices". Commented Mar 10, 2016 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." Commented Mar 10, 2016 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? Commented Mar 10, 2016 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
    Commented Mar 10, 2016 at 23:30

1 Answer 1

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?

Your Answer

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

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