3

Currently I have this and it works:

var markup = '<div>';
    markup += '<h1>This is a title</h1>';
    markup += '<p>This paragraph is really meaningful.</p>';
    markup += '</div>';
var div_list = document.getElementById('div_list');
        div_list.insertAdjacentHTML('beforeend', markup);

I want to move the markup to an html file (text.html). Then I want to assign the html from that file to a variable in my script.js file. The html file is located in the same folder as my script.js file. The main reason I'm doing this is to separate out my html into a file where I can work in html instead of adding all of the markup in my javascript file.

text.html contains the following:

<div>
    <h1>This is a title</h1>
    <p>This paragraph is really meaningful.</p>
</div>

I simply want to assign that text to a variable in my script.js file. This way I can do something like

var textFromFile = //whatever I have to do to assign the text from text.html
var div_list = document.getElementById('div_list');
    div_list.insertAdjacentHTML('beforeend', textFromFile);
badmidget
  • 53
  • 1
  • 7
  • 1
    I'm not sure this is a duplicate, it looks like they were asking for a way to use another html file, like an import (https://developer.mozilla.org/en-US/docs/Web/Web_Components/HTML_Imports). That feature is obsolete though, so a get request or server-side includes look like they're required. – GammaGames Sep 06 '19 at 14:22
  • You could also use template literals (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) in a js file and include them in a ` – GammaGames Sep 06 '19 at 14:25
  • 1
    https://jsfiddle.net/RokoCB/dLfgrze4/ – Roko C. Buljan Sep 06 '19 at 14:27
  • 1
    @RokoC.Buljan This is working. I have to figure out a logic issue with it, but it's pulling the html from my file and inserting it where I need it. So Thank you! – badmidget Sep 06 '19 at 14:47

0 Answers0