I want to build a web page by loading HTML elements from a separate file using jQuery.
Files I have:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script src="app.js"></script>
</body>
</html>
app.js
$.get("template.html",function(data) {
let a = $(data).contents().clone()
let b = a.find("#par1")
a.html()
$("body").append(b)
})
template.html
<template>
<div>
<p id="par1">apple</p>
<p id="par2">banana</p>
</div>
</template>
When I load index.html, par1 from template.html is loaded into the body, and the word apple is rendered. This is what I want, but I can't figure out why I need the line "a.html()" in app.js. If I comment it out, I get the error: "Uncaught TypeError: Cannot read property 'contains' of null" at the line with $("body").append(b). What is going on here?