David
David

Reputation: 1246

id attribute in html template tag

So, I'm diving into the HTML5 template tag, as a possible future solution to my web application problems. One thing that I don't seem to find is: Can you use the id attribute inside the template tag?

The template can be reused, but ids can't be. So using the same template multiple times (which is why they were invented in the first place) will result in invalid HTML, theoretically. Any official answers?

Upvotes: 5

Views: 3702

Answers (2)

Benny Powers
Benny Powers

Reputation: 5836

If you will stamp the template into the main document, the IDs must be unique, however, if you are stamping the template into a shadow root, then you can re-use IDs within the shadow DOM.

Upvotes: 0

Quentin
Quentin

Reputation: 943564

Can you use the id attribute inside the template tag.

Yes, however:

The template can be reused, but id's can't be.

Correct.

So using the same template multiple times (which is why they were invented in the first place) will result in invalid HTML, theoretically.

Yes. Don't do that. Make sure the IDs are unique before you add them to the main DOM. You could edit them in the template's DOM before you append it to the main DOM.

Upvotes: 8

Related Questions