<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>designing4u.de &#187; hide</title>
	<atom:link href="http://www.designing4u.de/tag/hide/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>jQuery &#8211; show / hide button for multiple div containers with AJAX and JSON</title>
		<link>http://www.designing4u.de/2008/07/jquery-show-hide-button-for-multiple-div-containers-with-ajax-and-json/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-show-hide-button-for-multiple-div-containers-with-ajax-and-json</link>
		<comments>http://www.designing4u.de/2008/07/jquery-show-hide-button-for-multiple-div-containers-with-ajax-and-json/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 14:02:46 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[boxes]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[multiple]]></category>
		<category><![CDATA[multiple div containers]]></category>
		<category><![CDATA[show]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=26</guid>
		<description><![CDATA[This time I will consider another case of loading content into div containers using jQuery, AJAX and JSON request. In an other example I showed you how you can simply load content into one div container using AJAX and provide a user show and hide button. In this example I want to consider a different [...]]]></description>
			<content:encoded><![CDATA[<p>This time I will consider another case of loading content into div containers using jQuery, AJAX and JSON request. In an other example I showed you how you can simply load content into one div container using AJAX and provide a user show and hide button. </p>
<p>In this example I want to consider a different case. Sometimes you don't only have one div container which needs to be filled out with content but multiple containers which need to be filled out with content according to the link which was clicked by the user. Again we will provide a functionality of changing the label of our button. We will extend this example by handling a larger amount of data in our response. For that reason we will use build in functionality of jQuery to handle JSON requests.<br />
<span id="more-26"></span><br />
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script><br />
<script type="text/javascript">
$(document).ready(function(){  
	$('.ahref',this).toggle(  
		function () {
			var id = $(this).attr('href');
			if($('.box'+id).text() == '') {  
				$.getJSON('/examples/multiple-div/json.php',{id:id},function(data){  
					$.each(data, function(i,item){  
						var user_name = item.user_name;
						var user_desc = item.user_desc;
						var html = user_name+" "+user_desc;
						$(".box"+item.id).html(html).slideDown(1000); 
					});  
				});  
				$(this).html("hide "+id);
			} else { 
				$(this).html('hide '+id);  
				$('.box'+id).slideDown(1000);  
			}  
		},  
		function () {  
			var id = $(this).attr('href');
			$(this).html('show '+id);  
			$('.box'+id).slideUp(1000);  
		}  
	);
});  
   </script><br />
After we will be done, our final solution should look more or less like that:</p>
<style type="text/css">
	.box1, .box2, .box3 {
		float:left;
	}
	.box1 {
		width:100px;
		height:100px;
		border:1px solid #000;
	}
	.box2 {
		width:100px;
		height:100px;
		border:1px solid #ff0000;
	}
	.box3 {
		width:100px;
		height:100px;
		border:1px solid #ff6600;
	}
	</style>
<p><a href="1" class="ahref">show 1</a><br />
<a href="2" class="ahref">show 2</a><br />
<a href="3" class="ahref">show 3</a></p>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div style="clear:left"></div>
<p>Here is the jQuery code we will use in this example.</p>
<p>1. Our jQuery code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.ahref'</span><span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggle</span><span style="color: #009900;">&#40;</span>
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.box'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'json.php'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>id<span style="color: #339933;">:</span>id<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
          $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  
            <span style="color: #003366; font-weight: bold;">var</span> user_name <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">user_name</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> user_desc <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">user_desc</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> html <span style="color: #339933;">=</span> user_name<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">+</span>user_desc<span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.box&quot;</span><span style="color: #339933;">+</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">id</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hide&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span> 
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hide'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.box'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideDown</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
      <span style="color: #009900;">&#125;</span>  
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>  
    <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
      <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'show'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.box'</span><span style="color: #339933;">+</span>id<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slideUp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span>  
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>2. Our HTML file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ahref&quot;</span>&gt;</span>show 1<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ahref&quot;</span>&gt;</span>show 2<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ahref&quot;</span>&gt;</span>show 3<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span> 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box3&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;clear:left&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>3. Our PHP file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">/**
 *  Database model for retrieving user data etc.
 */</span>
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'id'</span>        <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> 
    <span style="color: #0000ff;">'user_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ImUser '</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> 
    <span style="color: #0000ff;">'user_desc'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'description of a user '</span><span style="color: #339933;">.</span><span style="color: #000088;">$id</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' etc.'</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I don't want to waste your time, therefore I will directly move to explaining the code. We are using toggle to switch between functions each time a user clicks on a link. We use the href attribute of our link to save the corresponding id of an object we want to receive. You can use the href attribute to save all the data you can imagine, it is just important to handle it before processing to AJAX request.</p>
<p>In the first function we check, if our div box is empty, to prevent from loading the same content for multiple times. If our div container is empty we use AJAX to retrieve remote data and using the php function  json_encode we encode our array to a JSON object. We process the response to display it correctly for the end user and we paste it in corresponding div container. We slide it down to give a nice visual effect and replace the text of the link to "hide".</p>
<p>If our div box contains a text, we simply slide it down. Do not hesitate to delete the if else statement and load the content every time a user clicks a link however, if you have many visitors on your site, it might lead to performance issues.</p>
<p>In our second function we simply hide the content of the div container by sliding it up and we replace the text of the link to show. I'm defining the id of the link every time the function is called, but you can remove it. I'm just using it show you the id of the link you currently clicked on.</p>
<p>I hope this will help you farther with handling multiple div containers. I wrote this post really quickly and did not have time to check it, therefore if you find any mistakes, please inform me about them. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/07/jquery-show-hide-button-for-multiple-div-containers-with-ajax-and-json/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Loading content into multiple div containers using jQuery and JSON</title>
		<link>http://www.designing4u.de/2008/06/loading-content-into-multiple-div-containers-using-jquery-and-json/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=loading-content-into-multiple-div-containers-using-jquery-and-json</link>
		<comments>http://www.designing4u.de/2008/06/loading-content-into-multiple-div-containers-using-jquery-and-json/#comments</comments>
		<pubDate>Wed, 04 Jun 2008 13:35:09 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[JSON data]]></category>
		<category><![CDATA[multiple div containers]]></category>
		<category><![CDATA[show]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=24</guid>
		<description><![CDATA[Because I didn't write anything in a long time and because I just changed the layout of my site, I decided to write a new example about jQuery and extend it this time with JSON. After studying google analytics I noticed that people are mostly interested in jQuery examples and specifically in showing and hiding [...]]]></description>
			<content:encoded><![CDATA[<p>Because I didn't write anything in a long time and because I just changed the layout of my site, I decided to write a new example about jQuery and extend it this time with JSON. After studying google analytics I noticed that people are mostly interested in jQuery examples and specifically in showing and hiding content after users click. Because I already covered this example, I decided that this time I'll show you, how to load content into different div containers at one time. If you assume that you want to load content into more boxes, you definitely need to use JSON (JavaScript Object Notation). Of course one can say that XML would be suitable for this example as well, but the advantage of JSON is that it doesn't produce that much overhead. Of course XML is more versatile than JSON but with build in function of jQuery you can easily process received date.<br />
<span id="more-24"></span><br />
After we'll be done with this example, our final solution will look more or less like that:<br />
<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script><br />
<script type="text/javascript">
$(document).ready(function(){
  $('span').click(function() {	
    var boxes = $(this).attr('class');
    if($(this).html() == 'unload boxes '+boxes) {
      var empty = boxes.split(',');
      for(var i=0;i<5;i++) {
        $('.box'+empty[i]).html('');
        $(this).attr('id','')
      }
      $(this).html('load content box '+boxes);
    } else {
      $.getJSON('http://www.designing4u.de/examples/json/response.php', {boxes:boxes},function(data){
        $.each(data, function(i,item) {
          $(item.class).html(item.text);
        });
      });
      $(this).html('unload boxes '+boxes);
      $(this).attr('id','unload')
    }
  });
});
	</script></p>
<style type="text/css">
	.box1, .box2, .box3, .box4, .box5 {
		width:100px;
		height:30px;
		border:1px solid #000;
		float:left;
	}
	span {
		cursor:pointer;
		font-weight:bold;
	}
	#unload {
		color:red;
	}
	</style>
<p><span class="1,3,5">load content box 1,3,5</span><br />
<span class="2,4">load content box 2,4</span></p>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
<div style="clear:left;"></div>
<p>OK, let's break this code into pieces. First of all we need our HTML code, where our function can load the content. As usually, don't forget to include the jQuery library, because without nothing will really happen.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
.box1, .box2, .box3, .box4, .box5 {
	width:100px;
	height:100px;
	border:1px solid #000;
	float:left;
}
span {
	cursor:pointer;
	font-weight:bold;
}
#unload {
	color:red;
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1,3,5&quot;</span>&gt;</span>load content box 1,3,5<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2,4&quot;</span>&gt;</span>load content box 2,4<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">span</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box3&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box4&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;box5&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>I think this code is self explanatory. Our span elements will register an on click event and display the content in according boxes. The only thing you need to know is that class attribute defines the numbers of boxes, in which our content will be displayed (our first link will display content in box1, box3 and box5, therefore the name of the class is '1,3,5'). It's also important that you keep the names of the classes with according numbers in the div containers, because this code won't work either. If you decide to change them, don't forget to change it in the jQuery function as well. Let's move to the jQuery part.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> boxes <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We load our event listeners as soon as our DOM is loaded. In the first part of this code an event listener, which will be triggered whenever a user clicks on a span element. The first thing our function does is saving of the class value in variable 'boxes'.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> boxes <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'class'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'unload boxes '</span><span style="color: #339933;">+</span>boxes<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> empty <span style="color: #339933;">=</span> boxes.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span><span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.box'</span><span style="color: #339933;">+</span>empty<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#125;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load content box '</span><span style="color: #339933;">+</span>boxes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'json.php'</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span>boxes<span style="color: #339933;">:</span>boxes<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #003366; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'unload boxes '</span><span style="color: #339933;">+</span>boxes<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #339933;">,</span><span style="color: #3366CC;">'unload'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Full code might be a little bit tricky, therefore I'll try to explain it as good as possible. We add and if else statement. The first part of our statement will be responsible for unloading the boxes (simply removing the content out of them), the second part will do exactly the opposite. Let's start with explaining the second part first, cause that's the part, which will be executed first. We use an HTTP GET request to retrieve JSON data from an external file. In our case it will be json.php (content of this file will follow). As an addition we pass an argument boxes, which will tell our php file, for which boxes we want to load the content. After we receive the JSON data, we loop through it and paste it into according boxes. It is worth to mention in this moment that our script will receive a multi-dimensional array, in which we will save the classes of the boxes, in which we want to display our text and according texts. After our loop is done, we will replace the text of the link and add an id to give it a different color.</p>
<p>The first part of the script checks the text of the link, which was clicked by the user. If it contains the string, with which we replaced it, after loading of JSON data was done, we use again the class of our span attribute to remove the text from according boxes and we replace the text again.</p>
<p>The only thing left is the content of php file, which we used to create JSON data.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$boxes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'boxes'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$boxes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$box</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$push</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'class'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'.box'</span><span style="color: #339933;">.</span><span style="color: #000088;">$box</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'test for box '</span><span style="color: #339933;">.</span><span style="color: #000088;">$box</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span><span style="color: #000088;">$push</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In our php file we first initialize our array and we split the received data. For each value we received we extend our array adding a class name and corresponding text. In the last part we encode the array into JSON data and echo it. You can do all the magic you can imagine in this file. You can use IDs of the articles or users from your database and save it in the array. You can even extend this array and the jQuery function by extra fields.</p>
<p>One thing worth mentioning is that we use id attribute to change the color of our links. It is not really best practice, because multiple ids with the same name in one document wont validate with W3C rules. However, we do that on client side, therefore it shouldn't be an issue for a validation.</p>
<p>That's all. I hope you like this example. I know it's pretty custom and you will need to adjust it for your needs, but I hope it's clear enough for you to understand how JSON works and how to proceed retrived JSON data.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/06/loading-content-into-multiple-div-containers-using-jquery-and-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery &#8211; show / hide button with AJAX functionality</title>
		<link>http://www.designing4u.de/2008/05/jquery-show-hide-button-with-ajax-functionality/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery-show-hide-button-with-ajax-functionality</link>
		<comments>http://www.designing4u.de/2008/05/jquery-show-hide-button-with-ajax-functionality/#comments</comments>
		<pubDate>Thu, 15 May 2008 13:11:40 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[AJAX request]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[show]]></category>
		<category><![CDATA[toggle]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=11</guid>
		<description><![CDATA[Recently I had to provide a really simple and easy solution for showing and hiding some information, which should be triggered on users' click. I didn't have to think for a long time, how I will solve this problem and the framework I choose was obviously jQuery. In this example I will show you, how [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to provide a really simple and easy solution for showing and hiding some information, which should be triggered on users' click. I didn't have to think for a long time, how I will solve this problem and the framework I choose was obviously jQuery. In this example I will show you, how to trigger AJAX request and load some data into a div container and toggle between functions called every other click.<br />
<span id="more-11"></span><br />
Here is an example how our final solution will look like:<br />
<script src="http://code.jquery.com/jquery-latest.js"></script><br />
<script type="text/javascript">
	$(document).ready(function(){
		$('.link').toggle(
			function () {
				if($('#box').text() == '') {
					$.ajax({
						url:'http://www.designing4u.de/examples/jquery-show-hide/lorem.txt',
						method:'GET',
						success:function(r) {
							$('.link').html('hide');
							$('#box').html(r).hide().slideDown(1000);
						},
						error:function() {
							alert('file does not exist');
						}
					});
				} else {
					$(this).html('hide');
					$('#box').slideDown(1000);
				}
			},
			function () {
				$(this).html('show');
				$('#box').slideUp(1000);
			}
		);
	});
	</script></p>
<style type="text/css">
	#box {
		display:none;
		border:1px solid #000;
		width:500px;
	}
	.link {
		cursor:pointer;
		color:#ff0000;
		font-weight:bold;
	}
	</style>
<p><span class="link">show</span></p>
<div id="box"></div>
<p>Let's break it into pieces.<br />
[source:javascript]<br />
$(document).ready(function(){<br />
	$('.link').toggle(<br />
		function () {<br />
			if($('#box').text() == '') {<br />
				$.ajax({<br />
					url:'your_file.txt',<br />
					method:'GET',<br />
					success:function(r) {<br />
						$('.link').html('hide');<br />
						$('#box').html(r).hide().slideDown(1000);<br />
					},<br />
					error:function() {<br />
						alert('file does not exist');<br />
					}<br />
				});<br />
			} else {<br />
				$(this).html('hide');<br />
				$('#box').slideDown(1000);<br />
			}<br />
		},<br />
		function () {<br />
			$(this).html('show');<br />
			$('#box').slideUp(1000);<br />
		}<br />
	);<br />
});<br />
</script><br />
[/source]<br />
Because we want to write unobtrusive code, we want that our code should be executed whenever the DOM is ready. On each element with class 'link' we will toggle between two functions. In the first function we check, if our element with id 'box' is empty. If it is we use AJAX request to load the content from a remote file (in our case your_file.txt). For that reason you can use any other file. You can even pass parameters and request content relevant only for your case. For that reason you would have to add extra parameter 'data' in AJAX request of this function. </p>
<p>If the request was successful, we replace 'show' with 'hide', past the requested content in our container with id 'box', hide it and slide it down to get a nice visual effect. If this request retrieves an error, an alert box with information that the file wasn't found will be displayed. This function does the if - else check only because we want to do the AJAX request only once.</p>
<p>The second function is pretty simple. It basically replaces 'hide' with 'show' again and it slides up our div container. If a user clicks on 'show' button again, we wont need to use AJAX request anymore, because the text was already past in our DOM. Here is the HTML and CSS code for the body section of our document:<br />
[source:HTML]</p>
<style type="text/css">
#box {
	display:none;
	border:1px solid #000;
	width:250px;
}
.link {
	cursor:pointer;
	color:#ff0000;
	font-weight:bold;
}
</style>
<p><span class="link">show</span></p>
<div id="box"></div>
<p>[/source]<br />
If you want a nice effect of an arrow pointing up or down depending on the case, you can extend this function with some class replacements. Have fun with playing around with this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/jquery-show-hide-button-with-ajax-functionality/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

