settingsAccountsettings
Menusettings

Q: AJAX Load (task with AJAX and jQuery)

+3 votes

Use jQuery to write a JS function that loads an online resource into a div with id "text". Make a request to "text.html" and replace the target div’s content with the returned HTML. Create the files ajax-load.html and text.html and place them in the same folder, so that your script can find the latter.

HTML Skeleton:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AJAX Load Example</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<div id="text">
    <h1>AJAX jQuery.load()</h1>
    <button onclick="loadTitle()">Load Title</button>
</div>
</body>
</html>

text.html:

<h1>Voilla!</h1>
<p>I am a text loaded with AJAX request</p>

Examples:

ajax load task

asked in JavaScript category by user eiorgert

1 Answer

+2 votes

Here is the text.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>Voilla!</h1>
<p>I am a text loaded with AJAX request</p>
</body>
</html>

And the solution (HTML+JS):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
</head>
<body>
<div id="text">
    <h1>AJAX jQuery.load()</h1>
    <button onclick="loadTitle()">Load Title</button>
</div>

<script>
    function loadTitle() {
        $("#text").load("text.html");
    }
</script>
</body>
</html>

only the function:

function loadTitle() {
    $("#text").load("text.html");
}

 

answered by user hues
...