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

