Search code examples
javascripthtmltemplateshtml5-template

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?


Solution

  • 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.