Search code examples
htmlcomments

How to comment within an HTML attribute?


The final (closing) angle bracket in the code below is not interpreted as closing the <input> element:

<input type='text' name='name' <!-- id='name' -->>

I thought this was a valid way to comment out this attribute but Google Chrome, Firefox and Notepad++ (color coding) all suggest that this is not the way to go.

I used CTRL+Shift+Q in Notepad++ to do this.

Then what is the proper way to comment out this <id> attribute?


Solution

  • 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'>