Login
Your Email
Pasword
Home
Write
Trending
History
Liked
Earning

What is the id Attribute in HTML?

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

Do you have similar website/ Product?
Show in this page just for only $2 (for a month)
Create an Ad
0/60
0/180

The id value can be used by CSS and JavaScript to perform certain tasks for a unique element with the specified id value.
To select an element in css with a specific id you need to write a hash (#) character followed by the id of the element.
E.g:
<style>
#mycode {
 background-color: blue;
 color: White;
 padding: 30px;
 text-align: center;
}
</style>
<h1 id="myHeader">My Header</h1>


HTML element can only have one unique id that belongs to that single element while a class name can be used by multiple elements.
id Attribute in JavaScript
JavaScript can access an element with a specified id by using the getElementById() method.
E.g:
<script>
function displayResult() {
document.getElementById("mycode").innerHTML = "Have a nice nice coding";
}

The id value should contain at least one character and  not contain whitespace.

CONTINUE READING
id Attribute
JavaScript
HTML
Ayesha
Tech writer at newsandstory