JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 4th, 2008, 01:40 PM
imderek imderek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 54 imderek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 23 m 47 sec
Reputation Power: 5
JQuery: Hide/Show <P>

I have a page which contains a simple link and a paragraph below it:

html Code:
Original - html Code
    <a href="#" class="button">Display page content</a> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec tempor, enim quis convallis lobortis, sem enim auctor eros, sit amet ornare dui nunc sit amet nisl.</p>


I'd like jQuery to slide the paragraph down when the link is clicked (along with change the link text to "Hide page content"), and then slide the paragraph up when the link is clicked again (and change the link text back to "Display page content"). I've got the first part, but don't quite know how to proceed:

javascript Code:
Original - javascript Code
  1. $(document).ready(function(){
  2.     $("p").hide();
  3.     $("a.button").click(function() {
  4.         $(this).html('Hide page content');
  5.         $("p").slideDown('normal');
  6.     });
  7. });


Any help would be greatly appreciated.

Reply With Quote
  #2  
Old August 4th, 2008, 02:51 PM
derelict's Avatar
derelict derelict is offline
garish grotesque gargoyle
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
Posts: 1,337 derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 35 m 8 sec
Reputation Power: 1036
use one of jQuery's most basic pieces:

.toggle()

If you plan on using jQuery heavily, I strongly recommend that you read all available documentation and run through all available tutorials. I used jQuery for about a week and learned about .toggle() in the first hour. This question would have been answered almost immediately just by reading through the basic tutorial on the jQuery site.

good luck!

Reply With Quote
  #3  
Old August 4th, 2008, 03:17 PM
imderek imderek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 54 imderek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 23 m 47 sec
Reputation Power: 5
Thanks for the response, derelict. slideToggle is indeed the solution I was looking for.

javascript Code:
Original - javascript Code
  1. $("a.button").click(function() {
  2.     $("p").slideToggle('normal');
  3. });

The only thing left is how do I get the link text to change depending on whether or not the paragraph is displayed?

Reply With Quote
  #4  
Old August 4th, 2008, 03:35 PM
derelict's Avatar
derelict derelict is offline
garish grotesque gargoyle
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
Posts: 1,337 derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 35 m 8 sec
Reputation Power: 1036
please read my response above, and follow the link provided, to gain insight on how to use .toggle() to enforce a series of actions to be toggled every other click. toggle allows you to assign two different functions to be run, alternating on each click. use those functions to accomplish whatever you want your script to do.

Reply With Quote
  #5  
Old August 4th, 2008, 07:38 PM
imderek imderek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 54 imderek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 23 m 47 sec
Reputation Power: 5
I had a look at the link previously, and couldn't figure out how to use its methodology to create the desired effect.

Could you (or anyone) write an example of how .toggle() can show a <p> AND change the link text simultaneously? And then do the opposite when clicked again?

And please forgive my naivety with regard to JS :]

Reply With Quote
  #6  
Old August 5th, 2008, 10:38 AM
jsampsonPC's Avatar
jsampsonPC jsampsonPC is offline
stick a scissor in you eye
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Pensacola, Florida
Posts: 1,572 jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)jsampsonPC User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 16 h 19 m 9 sec
Reputation Power: 437
Derek,

The example was right on the page...

javascript Code:
Original - javascript Code
  1. $("p").toggle(function(){
  2.   $(this).addClass("selected");
  3. },function(){
  4.   $(this).removeClass("selected");
  5. });


Do you see the two functions, seperated by a comma? Put your "on" events within the first function, and the "off" events within the second function. The first will change text for a <p>, and the second will return the value of the text to its original state.

If this doesn't make sense to you, I suggest you go back and read the jQuery getting-started documentation to get more familiar with the syntax and structure of jQuery.

You could modify the provided code to change text in a link too:

javascript Code:
Original - javascript Code
  1. $("p").toggle(function(){
  2.   $(this).addClass("selected");
  3.   $("#myLink").html("I like apples!");
  4. },function(){
  5.   $(this).removeClass("selected");
  6.   $("#myLink").html("You don't like apples!?");
  7. });
__________________
FREE Web Development Videos | PHP Reference | MySQL Reference | jQuery Documentation

Markup Validation | CSS Validation | Web Developers, get FireBug & IE Developer Toolbar

When seeking help:
Submit clear questions and code. Use syntax highlighting.
Be patient, and respectful. Don't abuse your access to
professional guidance - nobody here is obligated to answer
your questions, be thankful that they do.

Are you a good person?

Last edited by jsampsonPC : August 5th, 2008 at 10:47 AM.

Reply With Quote
  #7  
Old August 5th, 2008, 11:51 AM
imderek imderek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 54 imderek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 23 m 47 sec
Reputation Power: 5
Thanks guys! I think I've finally got it.

javascript Code:
Original - javascript Code
  1. $(document).ready(function(){
  2.     $('p').hide();
  3.     $('a').toggle(function(){
  4.         $('p').show('slow');
  5.         $("#myLink").html("Hide page content");
  6.     },function(){
  7.         $('p').hide('slow');
  8.         $("#myLink").html("Display page content");
  9.     });
  10. });

Reply With Quote
  #8  
Old August 5th, 2008, 11:59 AM
derelict's Avatar
derelict derelict is offline
garish grotesque gargoyle
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
Posts: 1,337 derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 35 m 8 sec
Reputation Power: 1036
I think what you're missing here is that the .toggle() function replaces the .click() function...

think of .toggle() as the exact same thing as a .click() function, except it takes two functions and runs each alternately, every other click.

you would assign the .toggle() to the link to be clicked, and have the functions affect that link's text and whatever paragraph is to be affected.

good luck!
__________________

"Human history becomes more and more a race between education and catastrophe." (H.G. Wells)
"Giving me a new idea is like handing a cretin a loaded gun, but I do thank you anyhow, bang, bang." (Philip K. D!ck)

Reply With Quote
  #9  
Old August 5th, 2008, 12:09 PM
imderek imderek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 54 imderek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 23 m 47 sec
Reputation Power: 5
Quote:
Originally Posted by derelict
I think what you're missing here is that the .toggle() function replaces the .click() function..

Yep, that was the problem alright. Thanks again for the help!

Reply With Quote
  #10  
Old August 6th, 2008, 09:04 PM
imderek imderek is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2008
Posts: 54 imderek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 23 m 47 sec
Reputation Power: 5
Ok, here's another question for you. This time I've set it up so that mousing over the link shows the <p>, and once you move your mouse off of the link, the <p> is hidden. See http://www.imderek.com/dev/jquery.php.

javascript Code:
Original - javascript Code
  1. $(document).ready(function(){
  2.     $('p').hide();
  3.     $('a#toggleLink').mouseover(function(){
  4.         $('p').show('fast');
  5.     });
  6.     $('a#toggleLink').mouseout(function(){
  7.         $('p').hide('fast');
  8.     });
  9. });

html Code:
Original - html Code
    <a href="#" id="toggleLink">Toggle content</a> <p>Maecenas facilisis dictum ante. Duis ac nisl...</p>

The question is: how can I make it so that the <p> stays open if you move your mouse down from the link and into the <p>, and then finally hide it if the mouse moves off of it?

Reply With Quote
  #11  
Old August 6th, 2008, 09:37 PM
derelict's Avatar
derelict derelict is offline
garish grotesque gargoyle
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Mar 2006
Location: gracing gargantuan gothic gateways
Posts: 1,337 derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level)derelict User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 6 Days 8 h 35 m 8 sec
Reputation Power: 1036
that's a bit trickier... the way I've done this is with a global timeout before the p gets hidden.

here's the basic idea:

1) when the mouseover goes on the link, you check to see if a global timeout exists, and if so, kill it (so the p stays open). Then you make sure the p is shown, just as normal.

2) when you mouseout of the link, you don't hide the p immediately, instead you set a timeout, storing it in a global variable, to do the hiding.

3) you put these same functions on the p itself, so that mousing over it kills the timeout to hide it, and mousing out of it reinstates it.

you can set the timeout to be whatever duration suits you, usually about a half second to a second works well enough.

so now, on to the code:

Javascript Code:
Original - Javascript Code
  1. $(document).ready(function(){
  2.     $('p').hide();
  3.     // note we add 'p' into the selection range
  4.     $('a#toggleLink,p').mouseover(function(){
  5.         // and we check for out timeout, clearing if needed.
  6.         if(window.willhide) clearTimeout(window.willhide);
  7.         $('p').show('fast');
  8.     });
  9.     // again, selecting 'p' too
  10.     $('a#toggleLink,p').mouseout(function(){
  11.         // and setting that timeout to do the work for us
  12.         window.willhide = setTimeout(function() {
  13.             $('p').hide('fast');
  14.         },750); // with a 750 ms delay
  15.     });
  16. });


notice I used window.willhide as the timeout variable -- this is so it is stored globally (on the window object) so that it will be accessible by both functions. that should do what you want it to do; give it a shot and see. also look at the jquery documentation because there may be a more elegant way to do this with jquery. good luck!

Last edited by derelict : August 6th, 2008 at 09:38 PM. Reason: +p

Reply With Quote
  #12  
Old September 13th, 2012, 09:10 AM
mickyjune26 mickyjune26 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 13 mickyjune26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 16 sec
Reputation Power: 0
I followed the directions above, and it toggles. However, it does a weird bouncy thing when toggling. Is there a way to ensure smooth out the toggle?

Code:
<!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="assets/jquery-1.8.1.js"></script>
<script>
	$(document).ready(function(){
		$("#hidden-text").hide();
		$('a[id="toggle"]').toggle(function(){
			  $("#hidden-text").show('slow');
			  $('a[id="toggle"]').html("Hide Details");
			},function(){
			  $("#hidden-text").hide('slow');
			  $('a[id="toggle"]').html("Show details");
	  });
	});

</script>


</head>

<body>
<p>Good 'ol paragraph.  <a href="#" id="toggle">Show Details</a><br />

---------------------------------
<span style="display:none" class="hidden-text-class" id="hidden-text"><p>This is a paragraph with little content.</p></span> 
<p>-----------------------------The hidden text should display between the lines above.  
<br />This script is from http://forums.devshed.com/javascript-development-115/jquery-hide-show-p-548384.html</p>


</body>
</html>

Reply With Quote
  #13  
Old September 18th, 2012, 07:18 PM
mickyjune26 mickyjune26 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 13 mickyjune26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 16 sec
Reputation Power: 0
Found the solution - don't include any <p> tags. I just use <BR> a lot, and it is more clean in sliding in and out.

Reply With Quote
  #14  
Old September 18th, 2012, 09:16 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,830 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 17 h 9 m
Reputation Power: 4192
Please don't misuse <br> elements just because you've got a problem with your previous markup. I suspect the problem is caused by the margins on the <p>. Simply add a rule to your stylesheet to give "margin:0" to those paragraphs. If you still want some space to appear between the paragraphs, you could then give them some top and/or bottom padding.

P.S. Next time please start your own thread instead of reviving a very old one.
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #15  
Old September 19th, 2012, 07:53 AM
mickyjune26 mickyjune26 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2011
Posts: 13 mickyjune26 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 16 sec
Reputation Power: 0
Quote:
Originally Posted by Kravvitz
Please don't misuse <br> elements just because you've got a problem with your previous markup. I suspect the problem is caused by the margins on the <p>. Simply add a rule to your stylesheet to give "margin:0" to those paragraphs. If you still want some space to appear between the paragraphs, you could then give them some top and/or bottom padding.

P.S. Next time please start your own thread instead of reviving a very old one.


Thanks for the tip! All these CSS rules are tricky, but I'm starting to notice patterns of those that are referenced over and over! I may be getting closer to migrating away from tables and starting to use more DIVs!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > JQuery: Hide/Show <P>

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap