tadam logo

HTML id Attribute

Examples

Example 1

Use the id attribute to change a text with a JavaScript:
<html>
<head>
<script type="text/javascript">
function change_header()
{
document.getElementById("myHeader").innerHTML="Nice day!";
}
</script>
</head>

<body>
<h1 id="myHeader">Hello World!</h1>
<button onclick="change_header()">Change text</button>
</body>

</html>
The output of the code above will be:

(more examples at the bottom of this page)

Definition and Usage

The id attribute specifies a unique id for an HTML element.

The id must be unique within the HTML document.

The id attribute can be used by a JavaScript (via the HTML DOM) or by CSS to make changes or style the element with the specified id.

Support

W3C: The "W3C" column indicates whether or not the attribute is defined in the W3C HTML/XHTML recommendation.

Browser Support

Syntax

<element id="value">

Attribute Values

ValueDescription
idSpecifies a unique id for an element.

Naming rules:

  • Must begin with a letter A-Z or a-z
  • Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")
  • Values are case-sensitive

Try it Yourself - Examples

Add a unique id of element HTML
How to add the id attribute to an HTML element.

Use the id attribute on CSS
How to use the id attribute in CSS.

Was this information helpful?
   

Comments