Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create element from template element?

Tags:

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?

like image 221
Damien Roche Avatar asked Jul 09 '11 18:07

Damien Roche


2 Answers

Clone the object:

template = $("#template").clone(); template.attr("id","newid"); template.appendTo("body"); 
like image 57
Ujjwal Manandhar Avatar answered Sep 28 '22 07:09

Ujjwal Manandhar


The HTML5 provides a template element:

contents = $('#template').html(); copy = $('<div id="copy"></div>'); $('body').append(copy.append(contents)); 

The HTML part:

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

The clone method is not sufficient.

like image 27
manuel Avatar answered Sep 28 '22 07:09

manuel