Sartheris Stormhammer
Sartheris Stormhammer

Reputation: 2564

Loading a html snippet from another html file

I want to have an external .html file, from which I will load snippets programatically, and insert them in the document at runtime. I did it like this in my index.html

<head>
   <link rel="import" href="html/html_snippets.html">
</head>

This is the example content of the html_snippets.html file

<div id="asd">
   <some stuff here>
</div>

Then after everything is loaded, I load the snippet like this

var friendSnippet = $("#asd").html();

The problem I am facing now is that I use the jQuery code in a loop, which runs several times, and the first time it always returns an undefined, but after that it's fine.

Why is that happening?

Upvotes: 0

Views: 2047

Answers (1)

Mike Duister
Mike Duister

Reputation: 260

You can use the jQuery .load() function to load an html file into a certain element on the page.

$("#destinationElement").load("path/to/file.html");

This may solve the issue.

Upvotes: 2

Related Questions