Greg Gum Greg Gum - 3 months ago 11
HTML Question

Is there a reason not to use a custom "comment" attribute?

Is there a reason not to do the following?

<div comment="start display area">
...
</div>


I like to add notes, and this seems like an ideal way to do it.

I know I could do this as well:

<!--Start display area-->
<div">
...
</div>


The first syntax just seems cleaner to me. The issue I run into with
<!--
Is that sometimes, I do want to comment out an area of HTML for debugging purposes. That doesn't work when there are comment strings in there.

I found this much earlier discussion on it: https://www.w3.org/XML/2000/04schema-hacking/comment-test.html

Answer

You should use data attributes. Data attributes are designed exactly for what you're intending to do:

HTML5 is designed with extensibility in mind for data that should be associated with a particular element but need not have any defined meaning. data-* attributes allow us to store extra information on standard, semantic HTML elements without other hacks such as non-standard attributes, extra properties on DOM, or setUserData.

Your div would look like:

<div data-comment="start display area">