[Solved] Hiding a visible item with jQuery

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

[Solved] Hiding a visible item with jQuery

Post by social_experiment »

Sorry about the misleading subject line but i'm not sure how to word this in such a short space.

In the HTML code below, if i click on "Header One" then "Content One" is displayed, if i click on "Header Two" the text "Content One" slides up and the text "Content Two" slides down and is displayed. The Problem is, if i do click on "Header Two" again, the content slides up but immediately after closing, slides down again. How can i remedy this issue?

My jQuery code

Code: Select all

$(document).ready(function() {	
	$('h2.tandc_header').click(function() {
		$('p.tandc_content').slideUp();
		$(this).next('p.tandc_content').slideToggle();		
	});
});
My HTML code

Code: Select all

<h2 class="tandc_header">Header One</h2>
<p class="tandc_content">Content One</p>
<!-- -->
<h2 class="tandc_header">Header Two</h2>
<p class="tandc_content">Content Two</p>
Last edited by social_experiment on Thu Jan 26, 2012 5:01 am, edited 1 time in total.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Hiding a visible item with jQuery

Post by Celauran »

Code: Select all

$(document).ready(function() {
    $('p.tandc_content').hide();
    $('h2.tandc_header').click(function() {
        $(this).next('p.tandc_content').slideToggle();
        $(this).next('p.tandc_content').siblings('p.tandc_content').slideUp();
    });
});
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Hiding a visible item with jQuery

Post by social_experiment »

Thanks 8)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply