0

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>
StormTrooper
  • 1,731
  • 4
  • 23
  • 37
  • Does the tagged duplicate answer your question? If it doesn't please explain why not and explain with as much detail as possible why not. – zero298 Sep 08 '22 at 19:12
  • @zero298 isnt .load() a jquery function? I need this to be acheived by pure javascript – StormTrooper Sep 08 '22 at 19:14
  • The second answer to that question is pure JS with no jq and provides an example using `fetch` and `XHMHttpRequest`. – zero298 Sep 08 '22 at 19:15
  • @zero298 I am getting this issue: Reason: CORS request not http. Even though the file is in same directory – StormTrooper Sep 09 '22 at 05:47
  • That's a bit of a different question, but if you show exactly how you tried to load the HTML in your question including what URL scheme you used (`http://` vs `file://`) we might be able to help. There are generally more issues whenever you work on a site over `file://`. See ["Cross origin requests are only supported for HTTP." error when loading a local file](https://stackoverflow.com/q/10752055/691711) and consider using some local HTTP server if you aren't already. – zero298 Sep 09 '22 at 12:51

0 Answers0