How to include an html file in javascript

Viewed 20

I have a simple use case where I want to pick html from a seperate file and append it somewhere else after some operations in javascript.

forms.html

<div id="forms">
   <p>This is sample content of forms</p>
</div>

I want to include this file in my scripts & get its inner contents and append it to some other div.

scripts.js:

function drawFormContent(params){

    var str = '';
    str += '<div id="forms">';
    str += '<p>This is sample content of forms</p>';
    str += '</div>';
    // this str variable should be picked from forms.html file
    document.getElementById("mainContent").innerHTML = str;
}

Where my main page looks like this:

index.html

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" src="scripts.js"></script>
    </head>
    <body>
        
        <div id="mainContent"></div>
    </body>

    <script type="text/javascript">
        drawFormContent();
    </script>
</html>
0 Answers
Related