The id Attribute - Step 47

Hi there!

I’m a bit confused about the purpose of the id attribute…
Why is there a need to specify a unique identity for a given HTML element?

Thanks in advance :slight_smile:

Hello and welcome to the community :smiley:!
Using specific id attributes can be useful for times when you want to link two elements together by giving your first element a ‘for’ attribute with a value matching to your second element’s id attibute.

<label for="name">What is your name?</label>
<input id="name" type="text">

Now when the user click on the label ‘what is your name?’ they will get directed to the matching input field.
This might be useful:

1 Like

Thank you so much for your kind welcome and your quick response :slight_smile:

1 Like

her answer is somewhat strange.

id elements are usually used to link to anywhere on same page.

there was a trend where people would put everything on one page:
look at this template for example

this means that you’d link to sections on that page in navigation.
but in order to do that your sections need to have unique id element. so you’d link to that id and then it would slide to it.

same goes when going back to top <a href="#top">Go to top</a>

1 Like

Oh wow,

Thank you :slight_smile:

1 Like

I get what your saying, but why is my answer strange? Is it incorrect? Cause I’m pretty sure you can link specific elements together as well :thinking:. Am I mistaken?

No, it is not strange. Id’s are required in many cases to make a web page accessible. And there are also other reasons to use id’s that don’t have anything to do with accessibility. So your answer and @anon86258595’s answer are both good reasons for using id’s.