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
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
Hello and welcome to the community !
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.
Ex:
<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:
Thank you so much for your kind welcome and your quick response
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>
Oh wow,
Thank you
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 . 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.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.