<?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; div</title>
	<atom:link href="http://www.designing4u.de/tag/div/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>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>

