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?
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
});
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With