Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do partial page update with jQuery?

I have navigation menu. When clicked, only page content div should be updated from html content file (that in the server) without doing a full page refresh.

How can I achieve this using jQuery?

like image 620
DSharper Avatar asked Oct 23 '10 05:10

DSharper


2 Answers

Build your menu as per usual, i.e.

<ul id="menu">
    <li><a href="about-us.html">About Us</a>
    <li><a href="services.html">Services</a>
    <li><a href="products.html">Products</a>
</ul>

And a placeholder for the content.

<div id="content"></div>

Then run code similar to this

$("#menu li a").click(function(e) {
    // prevent from going to the page
    e.preventDefault();

    // get the href
    var href = $(this).attr("href");
    $("#content").load(href, function() {
        // do something after content has been loaded
    });
});
like image 86
Marko Avatar answered Oct 26 '22 00:10

Marko


You need to use the jQuery AJAX methods to accomplish this. There are several ways to go about it. For instance:

Say you have a div with id="mydiv" that you wish update with content from the server. Then:

  $("#mydiv").load("url");

will cause mydiv to be updated with the content returned from url.

This link describes various AJAX methods in jQuery.

like image 33
Vincent Ramdhanie Avatar answered Oct 25 '22 23:10

Vincent Ramdhanie