HTML id Attribute

HTML id Attribute: It can be defined as a unique id for an HTML element. And also used to identify a unique element and its content. It is used by CSS and JavaScript to perform a certain task for a unique element. In CSS, the id attribute is used using # symbol followed by id. While the id attribute value should not contain white space. Suppose if any two of the element has the same name, we can identify a single element by its id attribute.

HTML id Attribute

This attribute can be applied to all the HTML elements. And in HTML 4 the id value cannot start with a number. The id attribute does not supports with <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title> element. But in HTML5 the id attribute supports all the element.

Syntax: <element id=”value”>



Browser Support

This attribute is supported by the following browsers:

  • Chrome
  • Firefox
  • Opera
  • Safari
  • Internet Explorer

Example: for the id attribute

<!DOCTYPE HTML>
<html>
<body>
<h1 id="myHeader">Hello Freshersnow!</h1>
<button onclick="displayResult()">Change text</button>
<script>
function displayResult() {
  document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>

</body>
</html>

Output:

HTML id attribute

After clicking on change text

HTML id attribute 2