repeated jquery show/hide
Hello,
I'm trying to do what I think is a simple thing but I don't know what I'm doing wrong.
Here's what I want to do:
1. Have a link (p.advert a.toggle) inside p.advert that tells p.hidden to show itself and at the same moment hides that link (p.advert a.toggle).
2. p.hidden must be closed by a link within himself.
3. When I close p.hidden, I need p.advert a.toggle to become visible so that the user can see p.hidden again by clicking on p.advert a.toggle.
4. The code must be reusable many times on the same page.
At the moment I can't get point 3 to work.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Untitled Document</title>
- <script type="text/javascript" src="http://www.google.com/jsapi"></script>
- <script type="text/javascript">
- // You may specify partial version numbers, such as "1" or "1.3",
- // with the same result. Doing so will automatically load the
- // latest version matching that partial revision pattern
- // (e.g. 1.3 would load 1.3.2 today and 1 would load 1.4.2).
- google.load("jquery", "1.4.2");
-
- google.setOnLoadCallback(function() {
- // Place init code here instead of $(document).ready()
- });
- </script>
- <script>
- $(document).ready(function() {
- $('div.hidden').hide();
-
- $('div.show p.advert a.toggle').click(function() {
- $('div.show div.hidden').show('fast');
- $(this).hide();
- });
- $('div.show div.hidden a.toggle').click(function() {
- $(this).parent().hide('fast');
- $('div.show p.advert a.toggle').show();
- });
- });
- </script>
- </head>
- <body>
- <div class="show">
- <p class="advert">Sed ut perspiciatis voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? <a class="toggle">read more <img src="img/+.gif" /></a></p>
<div class="hidden">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aper. Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? <a class="toggle">less <img src="img/-.gif" /></a></p>
- </div>
- </div>
- </body>
- </html>
Thank you so much for any help