Filling multiple divs by single ajax call using jquery

Sometimes we need to fill two divs simultaneously using ajax. Generally , we use two different ajax calls to achieve this rather knowing the fact that it can also be done using single ajax call. One simple example is better than hundered words. Lets setup an example.

Here is the DEMO

HTML

Suppose we have two different divs having Ids div_1 and div_2 respectively.

1
2
3
    <div id="div_1"></div>
    <a href="" class="button">Click Me</a>
    <div id="div_2"></div>

.

AJAX file

The above divs to be filled simultaneously from ajax file having ids inner_1 and inner_2 respectively.

1
2
3
4
5
6
7
8
9
<div> <!-- This is empty div and please don't remove this -->
	<div id="inner_1">
		This is data from ajax file having id inner_1.
	</div>
 
	<div id="inner_2">
		This is data from same ajax file having id inner_2.
	</div>
</div>

.

Javascript

Now, here is the trick of jquery , how divs on main page is filled simultaneously. (jquery latest pack included)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js" language="javascript"></script>
	<script type="text/javascript" language="javascript">
	$( function() {
 
		$( '.button' ).click(function() {	
			var postData = ''"; // you can send any data to ajax file. 
			$('#div_1 , #div_2').html('<img src="ajax-loader.gif" />'); // placeholder
			$.ajax( {
				url : 'ajax_file.php', // your ajax file
				type : 'post',
				data : postData,				
				success : function( resp ) {
					$('#div_1').html($('#inner_1' , resp).html());
					$('#div_2').html($('#inner_2' , resp).html());
				}
			});
			   return false;
		});
	});
</script>

.

Here is the DEMO

All Done!
Cheers!!

Recently Published

»

Modern Changes That You Should Consider for Your Web Site

Websites need to modernize and get updated periodically otherwise ...

»

Super Tips For A Flawless Website

A website has a specific purpose. And your job is to design a website ...

»

Wix Offers Best Free Web Apps for Artists and Photographers

The famous online website builder Wix is known for providing the ...

»

What You Should Know Before Setting Up a Digital Marketing and Web Design Company

Running a digital marketing and web designing company can be a ...

»

Important Factors to Think About When Starting a Mailing List

Many bloggers and business owners don’t have a mailing list, ...

»

Tried and Tested Strategies for Boosting eBook Sales

As you have by now realized, writing your ebook was just the first ...

»

What Really Makes a Website User-Friendly

Whether it’s for business or personal use, having a website online ...

»

5 Features of Virtual Private Server that put you in complete Control of Your Website

For a decade now, virtual private server has been a preferred hosting ...

»

8 Ways the Right Ecommerce Platform Makes Your Business Perform Better

Starting your own ecommerce store is an exciting experience, but ...