Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML id Attribute

HTML id Attribute

HTML Global Attributes HTML Global Attributes

Example

Using the id attribute to manipulate a piece of text with JavaScript:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

title

<

script

function displayResult() { document.getElementById("myHeader").innerHTML="Have a nice day!"; }

script

head

<body>
<h1 id="myHeader">

Hello World!

h1

<button onclick="displayResult()">

Edit text

button

body

html

Try it »

More examples are included at the bottom of this section.


Browser support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the id attribute


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 as a link anchor, and to change or add styles to an element with the specified id through JavaScript (HTML DOM) or through CSS.


Differences between HTML 4.01 and HTML5

In HTML5, the id attribute can be used on any HTML element (it will validate any HTML element, but it may not be useful).

In HTML 4.01, the id attribute cannot be used on: , , , , ,