If there is something to add to the previous good answers, it is to explain why id
s must be unique per page. This is important to understand for a beginner because applying the same id
to multiple elements within the same page will not trigger any error and rather has the same effects as a class
.
So from an HTML/CSS perspective, the uniqueness of id
per page does not make a sens. But from the JavaScript perspective, it is important to have one id
per element per page because getElementById() identifies, as its name suggests, elements by their id
s.
So even if you are a pure HTML/CSS developer, you must respect the uniqueness aspect of id
s per page for two good reasons:
- Clarity: whenever you see an id you are sure it does not exist elsewhere within the same page
- Scalability: Even if you are developing only in HTML/CSS, you need to take in consideration the day where you or an other developer will move on to maintain and add functionality to your website in JavaScript.