Yukatanic45 Yukatanic45 - 1 year ago 78
HTML Question

How to place the content of a file in a div with Javascript?

I would like to place the content of a file inside a

, and not using IDs or classes, only with the
tag inside the
This is my code:

<span>hi, welcome to the </span>
// This is what I need
<span> season...</span>

Answer Source

You can use jQuery AJAX to load a file and use the document.write() function to place the content in that gap. Code:

    <script src="https://code.jquery.com/jquery-3.1.0.js"></script>

        <span>hi, welcome to the&nbsp;</span>
                async: false, // With this, you wait until the file is loaded
                type: "GET", // If the file is large, use POST instead
                url: "yoururl.txt", // Use the relative URL
                dataType: "text",
                success: function(LoadedFile) {

NOTE: Use the &nbsp; instead of the space " " to force your browser to use them.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download