How to comment within an HTML attribute?

HTML provides no way to place a comment inside a tag.


If you are generating the HTML from a template / programming language, then you can use features of that to comment something out.

For example, in Template-Toolkit:

<input type='text' name='name' [%# id='name' %]>

or PHP:

<input type='text' name='name' <?php # id='name' ?>>

If you are using HTML 5 then you could (as an ugly hack) use a data attribute to "comment" out entire attributes.

<input type='text' name='name' data-comment-id='name'>

I usually just put _x at the end of the attribute name. Then the attribute is ignored because it's unknown. So if I wanted to comment out the id attribute from this element:

<input type="text" name="name" id="name">

I would change it to this:

<input type="text" name="name" id_x="name">

This also has the advantage of being able to search for "_x=" to find all commented attributes.

Tags:

Html

Comments