jQuery and html 5 <template> tag

3.9k views Asked by At

I have some problem with jQuery. When am trying use:

document.querySelector('#container').appendChild(content.cloneNode(true));

it's ok, but jquery have wrapper around the object, so i can't just type:

$('#container').content

Does anyone have any ideas?

2

There are 2 answers

0
nrabinowitz On BEST ANSWER

If you want to access the actual DOM element for a single-element selection, use $('#container')[0] or $('#container').get(0).

If you had multiple elements in your selection, you could get them by index: .get(1), .get(2) and so on.

3
manuel On

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.