34

For example:

<a /*title="need to be comment out"*/>a link</a>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lovespring
  • 19,051
  • 42
  • 103
  • 153
  • Is there a reason you only want to comment them out? Do you need them to exist afterwards? If not you could completely remove them. – Pit Jan 20 '11 at 07:19
  • 1
    @Pit Just like coding other languages. sometimes, I need to comment it out for testing. – lovespring Jan 20 '11 at 07:22
  • On mac (using the program ; Coda) you can simply press CMD + / after selecting the items ofcourse – Marco Geertsma Jun 12 '13 at 13:00
  • A workaround is to have a script (or similar) strip out (remove) such comments before other parts of the system sees it. I did it for [WiX](https://en.wikipedia.org/wiki/WiX) source code (pure XML) to able to use comments inside tags. The script was the first to be called by the build process. Per-line comments (`//`) were also enabled by this. – Peter Mortensen Feb 24 '22 at 20:03
  • Related (using `<!`): *[Does HTML5 change the standard for HTML commenting?](https://stackoverflow.com/questions/29405858/)* – Peter Mortensen Mar 15 '22 at 13:19

4 Answers4

42

The W3C documentation suggests it cannot be done:

Note that comments are markup.

This basically means that a <!-- ...> comment tag is just like any other tag, so <a <!--title="need to be comment out"-->>a link</a> is as wrong as <a <span></span>>a link</a>.

For quick hacking I believe a common option is to rename that attribute. While you obtain invalid HTML, you can temporarily remove the attribute:

<a xtitle="need to be comment out">a link</a>

If you happen to be using a server-side language, you can also use its own comment syntax. For instance, in PHP you can do this:

<a <?php/*title="need to be comment out"*/?>>a link</a>

... which generates this HTML:

<a >a link</a>

... and in ASP.NET you can use <%-- Comment goes here --%> while the ASP.NET MVC Razor syntax is @* Comment goes here *@.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 2
    You might want to make it more obvious you have purposely "commented" the attribute out. For instance with comment_out_title="...". It may be assumed to be a mispelling. – Matthew Wilcoxson Aug 23 '12 at 13:20
  • 1
    You do not obtain invalid HTML if you use `data-` attributes, for example, `a link`, though many best practices, including those by such big frameworks as Angular, disadvise this. – trysis Sep 25 '16 at 13:41
  • A minor thing, but in PHP, you can remove the space, as well, with, for example, `>a link`. – trysis Sep 25 '16 at 13:43
7

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.

intrepidis
  • 2,870
  • 1
  • 34
  • 36
5

You can't. Comments can only start and end outside tags.

Some people preprend an x to an attribute name, thus changing it and causing it to be all but ignored (since it is still often visible in the DOM), but this is invalid.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

This cannot be done, but an attribute can be removed via the removeAttribute(attribute_name) JavaScript call.

Alternatively, you can prefix the attributes you want removed with a namespace like <a nosuchns:title="nevershown">click</a> and remove the namespace via JavaScript.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Horia Dragomir
  • 2,858
  • 24
  • 21