Search code examples
javascripthtmlajaxpage-refresh

How do I refresh a div with AJAX without refreshing the whole page?


I am trying with this JS:

        function refresh () { 
        $.ajax({
            url: window.location.href,
            success: function(data) {
                $('.torefreshdiv').html(data);
            }
        });
    }

But this is loading the whole page inside the div, but i would like to refresh only the '.torefreshdiv'.
I thank everyone who helps me in advance.


Solution

  • You can create another resource for the content of the <div> to refresh (that can be another php script if using PHP, or the same script as the whole page but with some params...). Then:

    function refresh () {
      $.ajax({
        url: '/div/to/refresh.php',
        success: function(data) {
          $('.torefreshdiv').html(data);
        }
      });
    }