Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  74] [ 6]  / answers: 1 / hits: 21604  / 10 Years ago, fri, december 5, 2014, 12:00:00

I have two similar selections. The first uses a <div> tag, which works fine, the second uses a newly <template> tag, which doesn't work anymore.



Can anyone tell me how to get this to work with jQuery using the <template> tag?



HTML



<div id=div>
<div>content</div>
</div>

<template id=template>
<div>content</div>
</template>


JavaScript



var $div = $('#div');
var $content = $div.find('div');
console.log($content); //works ($content.length == 1)

var $template = $('#template');
var $content = $template.find('div');
console.log($content); //doesn't work ($content.length == 0)





http://jsfiddle.net/s8b5w0Le/1/


More From » jquery

 Answers
41

HTMLTemplateElement saves the DOM into a seperate attribute:



JQuery



<script src=jquery-3.1.0.js></script>
<script type=text/javascript>
$(document).ready(function()
{
var $div = $('#div');
var $content = $div.find('div');
console.log($content.text()); // output content, inner div

var $template = $('#template');
var node = $template.prop('content');
var $content = $(node).find('div');
console.log($content.text()); // output content, inner template
});


JavaScript



document.createElement('template').content

[#68580] Wednesday, December 3, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
collinisaaka

Total Points: 194
Total Questions: 105
Total Answers: 104

Location: Tonga
Member since Tue, Nov 30, 2021
3 Years ago
;