What is purpose of id attribute in html?

For example:


<a id="value">text</a>

The id attribute specifies a unique id for an HTML element. Suppose you have more than one <div></div> in html file. When you call them from stylesheet it will be hard to identify them.
But if you declare a <div></div> like this
<div id=”maindiv”>
</div>
That will be easy for your coding and also for identify.
The id attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.

In this case you are creating a place on the page called value; you can than have a link lower down the page ( or from another page ) that when the link is clicked the page will load with the line containing value at the top.

See this page.

The id attribute specifies a unique id for an HTML element.
This is important , unlike the class attribute, you SHOULD NOT apply the value of the ID attribute to any other element in that same HTML doc.

The ID attribute can be used in any of 3 ways:

  1. AN ANCHOR. Let’s say you want to navigate within the same document. <div id=“target”>…</div><a href=“your.uri/HTMLdoc.html#target”>…</a>
  2. As a QUICK hook for .js, using document.getElementByID(‘target’);
  3. As a heavy weight CSS hook #target { color:red;}. To understand this better you may want to read up on CSS specificity.

hope that helps

Id attribute is unique identification so that it is used for one tag and we can specify the style for that tag in HTML document.

There is no reason for using an <a> tag for that any more - if you see that in a page then it is because the code either hasn’t been fully updated since Netscape 4 died (which used to require a name in that tag as well) or the person who wrote it doesn’t fully understand HTML.

The id can go in any tag and be used as the destination of a link without the tag it is in needing to be an <a> so simply move the id to the nearest tag and get rid of the <a>

The id attribute is which gives the unique id for elements in HTML.

ID attribute is a unique elements in html, it can be used for certain situation, such as javascript validation(getelementbyid) or css attribute.(#id attribute)

Thanks everyone, I guess we have enough answers to the simple question.

Topic Closed