Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
jQuery 3.6.0 can't parse "template" tag

The other day I's working on a project using jQuery 3.6.0 but I found that it can't parse <template> tags.

Refer to the below example.

const temp = $("#main");
console.log(temp.find("div").html()); // this isn't working


const temp2 = $("#main2");
console.log(temp2.find("div").html()); // this is working
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<template id="main">
  <div class="first">
    <span>Hola</span>
  </div>
</template>


<div id="main2" style="display:none;">
  <div class="first">
    <span>Hola</span>
  </div>
</div>

What am I doing wrong here?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

template results in a document fragment in which the contents of the template element is. You have to access it using content.

So what you could do is $(temp[0].content).find('div').html()

But you need to be careful about that MDN: <template>: The Content Template element:

However, the HTMLTemplateElement has a content property, which is a read-only DocumentFragment containing the DOM subtree which the template represents. Note that directly using the value of the content could lead to unexpected behavior, see Avoiding DocumentFragment pitfall section below.

const temp = $("#main");
console.log($(temp[0].content).find('div').html()); // this isn't working


const temp2 = $("#main2");
console.log(temp2.find("div").html()); // this is working
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<template id="main">
  <div class="first">
    <span>Hola</span>
  </div>
</template>


<div id="main2" style="display:none;">
  <div class="first">
    <span>Hola</span>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!