Auine
Auine

Reputation: 200

jQuery and html 5 <template> tag

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?

Upvotes: 2

Views: 3983

Answers (2)

manuel
manuel

Reputation: 217

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.

Upvotes: 3

nrabinowitz
nrabinowitz

Reputation: 55678

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.

Upvotes: 1

Related Questions