/    Sign up×
Community /Pin to ProfileBookmark

jQuery show/hide help

I have a question regarding jQuery show/hide.

At the moment, my page opens and my FAQs have all the answers showing. When I click on any of the questions, all the answers hide.

What adjustments can I make that will have my page open to only showing the questions (this is my FAQ page) and when a visitor clicks on a question, only that answer shows? Right now all answers show when I click a question.

thank you for the help,
Randy

My jquery code is this:

[CODE]
$(document).ready(function () {
$(‘h3’).click(function() {
if ( $(‘li p.faqs_answers’).is(‘:visible’)){
$(‘li p.faqs_answers’).hide(1000);
}else {
$(‘li p.faqs_answers’).show(1000);
}
});
});
[/CODE]

And a bit of my html code is this:

[CODE]
<div id=”faqscontactaboutservices”>

<h3 class=”faqs_specific”>Frequently Asked Questions</h3>
<ul>
<li>
<div class=”faqs_specific”><h3>&nbsp;What areas do you serve?</h3></div>
<p class=”faqs_answers”>answer goes here</p>
</li>
<li>
<div class=”faqs_specific”><h3>&nbsp;What forms of payment do you accept?</h3></div>
<p class=”faqs_answers”>We accept cash, check, debit card, and credit card.</p>
</li>


[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@holyhttpJan 10.2013 — You should give each answer section a different CSS id so you can determine which DIVs to reveal or hide.
Copy linkTweet thisAlerts:
@tuxandpucksauthorJan 11.2013 — Thanks, however, I wasn't successful. Does anyone have an explicit example? thanks.
Copy linkTweet thisAlerts:
@PadonakJan 11.2013 — &lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;title&lt;/title&gt;
&lt;script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"&gt;&lt;/script&gt;
&lt;style type="text/css"&gt;
div.faqs_specific h3{cursor:pointer;}
p.faqs_answers{display:none;}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
$(document).ready(function(){
$('div[class="faqs_specific"]').click(function(){
var par=$(this).next();
/*this one is for hiding any previous answer which may be still visible*/
$('p[class="faqs_answers"]').each(function(){if($(this)!==par){$(this).hide(100);}});
if(par.is(':visible')){par.hide(200);}
else{par.show(300);}
});
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="faqscontactaboutservices"&gt;
&lt;h3 class="faqs_specific"&gt;Frequently Asked Questions&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;div class="faqs_specific"&gt;&lt;h3&gt;&amp;nbsp;What areas do you serve?&lt;/h3&gt;&lt;/div&gt;
&lt;p class="faqs_answers"&gt;answer goes here&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class="faqs_specific"&gt;&lt;h3&gt;&amp;nbsp;What forms of payment do you accept?&lt;/h3&gt;&lt;/div&gt;
&lt;p class="faqs_answers"&gt;We accept cash, check, debit card, and credit card.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Ay__351_eJan 11.2013 —  <br/>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;style type="text/css"&gt;
.active {color:red;};
&lt;/style&gt;

&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

&lt;script type="text/javascript"&gt;
// http://api.jquery.com/toggleClass/
// http://www.akifkaraagac.com/addclass-removeclass-toggleclass-jquery-css-isemleri/

$(document).ready(function(){
$(".hidden").hide();
$("h2.trigger").click(function(){

$(".hidden").hide();

<i> </i> $(this).next(".hidden").slideToggle("slow,");
<i> </i>});
});
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;h2 class="trigger"&gt;The Time Through Ages&lt;/h2&gt;
&lt;div class="hidden"&gt;
In the Name of Allah, Most Gracious, Most Merciful
1. By the Time,
2. Verily Man is in loss,
3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.
&lt;/div&gt;
&lt;h2 class="trigger"&gt;The Opening&lt;/h2&gt;
&lt;div class="hidden"&gt;
1. In the name of Allah, Most Gracious, Most Merciful.

2. Praise be to Allah, the Cherisher and Sustainer of the worlds;

3. Most Gracious, Most Merciful;

4. Master of the Day of Judgment.

5. Thee do we worship, and Thine aid we seek.

6. Show us the straight way,

7. The way of those on whom Thou hast bestowed Thy Grace, those whose (portion) is not wrath, and who go not astray.

&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@MacPCJan 12.2013 — Right off my head, you are asking JQ to response to all h3 elements! :eek:

$(document).ready(function () {

[COLOR="#FF0000"]$('h3').click(function() [/COLOR]{

if ( $('li p.faqs_answers').is(':visible')){

$('li p.faqs_answers').hide(1000);

}else {

$('li p.faqs_answers').show(1000);

}

});

});

Incidentally, I was recently asked to fix a website, the website has a lot of large images, the initial load took forever, the original developer made the same mistake, he did a preload of all images with $('img'), so all 200 or so images were loading when the page starts!!!!! It took >40sec to just for the front page to show up!!!!! :0
×

Success!

Help @tuxandpucks spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...