In this article I will show you how to change the contents of a div without refreshing the whole page using jQuery.
Let us create view first :
- <div class="container">
- <div class="container-left">
- <ul>
- <li> <a id="one" href="#">About Company</a></li>
- <li><a id="two" href="#">Our Products</a></li>
- <li><a id="three" href="#">Our Services</a></li>
- </ul>
- </div>
- <div class="container-right">
- <h2>Welcome to Some Company Name</h2> </div>
- </div>
There is one main div called container(class name) and two sub divs with classnames container-left and contaner-right. Container-left has 3 links, with ids one, two and three.This acts as a content changer.content-right div is empty, initially I have inluded an h2 tag(Welcome to Some Company Name).
Here is the Css for the page : - body
- {
- background: #DDD;
- }.container
- {
- width: 80 % ;padding: 10 px;
- }.container - left, .container - right
- {
- background: #FFF;padding: 25 px;border: solid 1 px# CCC;float: left;
- }.container - left
- {
- width: 20 % ;
- }.container - right
- {
- width: 70 % ;
- }
Now let us see the jquery part :
- First I have included the latest jquery library(cdn) :
- <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
- Inslude the $(document).ready() frunction i have create three variables called p1,p2,p3.In each variable I have stored the html contents. (Here I have stored dummy contents)/
- var p1 = "<h2>About Company</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>";
- var p2 = "<h2>Our Products</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>";
- var p3 = "<h2>Our Services</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,</p>";
- Then I have created event handler click function for each links having id : one,two,three.Inside each click function ,i used the method html() to change the innerHTML of content-right div.
- $("#one").click(function()
- {
- $(".container-right").html(p1);
- });
- $("#two").click(function()
- {
- $(".container-right").html(p2);
- });
- $("#three").click(function()
- {
- $(".container-right").html(p3);
- });
Here #one, #two and #three is the id of each link. And content-right is the classname of the div that we want to change the inner html.When we click on any of the links, click function for the corresponding link will execute and it will change the innerHTML of the content-right div with correspoding contents(p1,p2,p3);
Instead of storing the contents inside the variable we can use database to store, and it can be fetched from database using any serverside programming language using ajax call.
Here is the full code and Screenshot :
- <html>
-
- <head>
- <title>Content Changing using jQuery</title>
- <style type="text/css">
- body
- {
- background: #DDD;
- }
-
- .container
- {
- width: 80%;
- padding: 10px;
- }
-
- .container-left,
- .container-right
- {
- background: #FFF;
- padding: 25px;
- border: solid 1px #CCC;
- float: left;
- }
-
- .container-left {
- width: 20%;
- }
-
- .container-right {
- width: 70%;
- }
- </style>
- <script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function()
- {
- var p1 = "<h2>About Company</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>";
- var p2 = "<h2>Our Products</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.</p>";
- var p3 = "<h2>Our Services</h2><p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting,</p>";
- $("#one").click(function()
- {
- $(".container-right").html(p1);
- });
- $("#two").click(function() {
- $(".container-right").html(p2);
- });
- $("#three").click(function() {
- $(".container-right").html(p3);
- });
- }); < /script> < head > < body > < div class = "container" > < div class = "container-left" > < ul > < li > < a id = "one"
- href = "#" > About Company < /a></li > < li > < a id = "two"
- href = "#" > Our Products < /a></li > < li > < a id = "three"
- href = "#" > Our Services < /a></li > < /ul> < /div > < div class = "container-right" > < h2 > Welcome to Some Company Name < /h2> < /div > < /div> < /body > < /html>
Thank you,if you have any doubts, please feel free to ask me.
Read more articles on jQuery: