11

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?

2
  • Why comment? remove if its not in use Commented Dec 24, 2012 at 11:03
  • Just remove the id attribute? Commented Dec 24, 2012 at 11:03

5 Answers 5

21

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'>
Sign up to request clarification or add additional context in comments.

3 Comments

that really is a bummer. It would be nice to temporarily disable attributes like "class" or "id" in some cases.
Thx, I'm actually learning PHP but this one was hand-written!
The initial goal was to save myself typing by using the Notepad++ shortkey for commenting out stuff. Works great for CSS and PHP but apparently not always for HTML :-(
3

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.

1 Comment

Obviously this trick should only be used when developing and "commented" attributes shouldn't be left in production code.
0
<input type='text' name='name' <?php  /* id='name' */ ?> >

you may use this it will not be interpreted when viewing the source info

Comments

-2

I agree, that you should not use comments at this place. That said, the following should work in Chrome, Firefox and IE:

<input type="text" %id="test1"% class="test2">

Inspect element in Google Chrome

Inspect element in Firefox

2 Comments

That puts <input type="text" %="" %id="test1"> in the DOM, which is horrible.
In Chrome it renders <input type="text" %id="test1" % class="test2">.
-2

If you want to comment a line in HTML5 and don't use

<!-- Html comments -->

You can use

  • aria-label

for example:

 <button aria-label="Close" nclick="myDialog.close()">X</button>

or

<button aria-label="This button close the popup" nclick="myDialog.close()">X</button>

Since there is nothing to indicate that the purpose of the button is to close the dialog, the aria-label attribute is used to provide a label to any assistive technology or simply comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.