Alex Rivera | Logout

Create element from template element?

Asked 2011-07-09T18:20:23.467
25

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?

Edit
Report

1 Answer

2

You can use the clone() method like so:

template = $('#template').clone();
template.attr('id', 'newid');
template.appendTo('body');

Look at http://api.jquery.com/clone/ for more info.

answered 2011-07-09T18:24:40.307

Your Answer