Create element from template element?

Viewed 22857

I'm having some trouble with this:

template = $("#template");
$(template).attr("id", "newid");

$(template).appendTo("body");

What I want to do is assign an id to the template, then amend the content. Trouble is, I am currently referring to the actual template element, and so the id is changing that. On using this template again, I cannot select as the id is different.

Any advice on optimal approach?

6 Answers

For me clone() didn't work with <template>. Best answer was the one from Manuel, but to generate a JQuery object from template without adding an element as he did, I used this one-liner:

var template = $($("#template").html());

with this html

<html>
 <body>
  <template id='template'>
  </template>
 </body>
</html>
Related