Atribut id HTML
Contoh
Gunakan atribut id untuk memanipulasi teks dengan JavaScript:
<html>
<body>
<h1 id="myHeader">Hello World!</h1>
<button onclick="displayResult()">Change text</button>
<script>
function displayResult()
{
document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>
</body>
</html>
Lebih banyak contoh "Coba Sendiri" di bawah ini.
Definisi dan Penggunaan
Atribut id
menentukan id unik untuk elemen HTML (nilainya harus unik di dalam dokumen HTML).
Atribut id
ini paling sering digunakan untuk menunjuk ke gaya dalam lembar gaya, dan oleh JavaScript (melalui DOM HTML) untuk memanipulasi elemen dengan id tertentu.
Dukungan Peramban
Attribute | |||||
---|---|---|---|---|---|
id | Yes | Yes | Yes | Yes | Yes |
Sintaksis
<element id="id">
Nilai Atribut
Value | Description |
---|---|
id | Specifies a unique id for the element.
Naming rules:
|
Lebih Banyak Contoh
Contoh 1
Gunakan atribut id untuk menautkan ke elemen dengan id tertentu di dalam halaman:
<html>
<body>
<h2><a id="top">Some heading</a></h2>
<p>Lots of text....</p>
<p>Lots of text....</p>
<p>Lots of text....</p>
<a href="#top">Go to top</a>
</body>
</html>
Contoh 2
Gunakan atribut id untuk menata teks dengan CSS:
<html>
<head>
<style>
#myHeader {
color: red;
text-align: center;
}
</style>
</head>
<body>
<h1 id="myHeader">W3Schools is the best!</h1>
</body>
</html>
Halaman Terkait
Tutorial HTML: ID HTML
Tutorial HTML: Atribut HTML
Tutorial CSS: Sintaks CSS
Referensi CSS: Pemilih #id CSS
Referensi DOM HTML: Metode getElementById() HTML DOM
Referensi DOM HTML: Properti id DOM HTML
Referensi DOM HTML: Objek Gaya DOM HTML