How to refresh DIV content using jQuery

← PrevNext →

You can reload or refresh an entire web page automatically using jQuery. That’s simple. Now, how do you refresh only the contents inside a DIV element (or any element)? In jQuery, you can use the setInterval() function to refresh or load new contents inside a DIV element at a given interval. I’ll show you how this is done.

There are plenty of other examples on jQuery with XML and you can read all of it here.

Now, there can be multiple DIV’s with different contents on a single page and we can refresh all the DIV’s contents at a specified time interval. We can set different time intervals for different DIV and refresh each without reloading the page.

Here in my example, I have an XML file (library.xml) with few nodes in it. I’ll extract each node (with values) every 5 seconds and display the contents in the DIV. That is, the DIV gets refreshed with new values, extracted from the XML file every 5000 milliseconds.

See this demo

👉 Now, you can also do this using plain JavaScript. See this example here.
Auto refresh page using JavaScript


Copy the data and save it in file and name it library.xml.

<?xml version="1.0"?>
<!-- Last edited by Arun Banik @ https://encodedna.com -->
    <BookName>Computer Architecture</BookName>
    <BookName>Advanced Composite Materials</BookName>
    <BookName>Asp.Net 4 Blue Book</BookName>
The Markup and the Script
<!DOCTYPE html>
    <title>Refresh a DIV Without Reloading the Page</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <p>Will refresh content in 5 seconds</p>
    <div id="books" style="margin:5px 0;"></div>

    $(document).ready(function () {

    var i = 1;
    function refresh_Div() {

            type: 'GET',
            url: '../../library/library.xml',
            dataType: 'xml',
            success: function (xml) {

                $(xml).find('List').each(function () {

                    if ($(this).find('code').text() == i) {
                        '<div>' +
                            '<div><b>Name of Book: </b>' +
                                $(this).find('BookName').text() + '</div> ' +
                            '<div><b>Category: </b>' +
                                $(this).find('Category').text() + '</div> ' +
                            '<div><b>Price: </b>' +
                                $(this).find('Price').text() + '</div> ' +

                i = i + 1;
                if (i >= 3) i = 1;

    var reloadXML = setInterval(refresh_Div, 5000);
Try it

In the above script, the setInterval() method calls the refresh_Div() method every 5 seconds (5000 milliseconds). The method has an Ajax code that will extract XML data, node by node in every 5 seconds and append it to the DIV, without reloading the page.

Similarly, you can display images (like an automatic thumbnail slider) and show the images one by one on a DIV, without reloading the page and other contents.

That’s it. Hope you like this article and its example. Thanks for reading 🙂.

See this demo

← PreviousNext →