drenl
drenl

Reputation: 1333

jquery toggle content by header

for the following HTML:

 <h1 id="t1">First Title</h1>
 <p class="contents" id="c1">First Content</p>

 <h1 id="t2">Second Title</h1>
 <p class="contents" id="c2">Second Content</p>

 <h1 id="t3">Third Title</h1>
 <p class="contents" id="c3">Third Content</p>

<!-- etc... -->

I'd like to use jquery to slideToggle the content for each specific header. Ie: clicking on id="t2", I'd like "c2" to toggle.

Thanks for your suggestions or code!

Upvotes: 0

Views: 101

Answers (2)

Tyro Hunter
Tyro Hunter

Reputation: 755

use class for your h1 instead, since it fits much better in your requirement..

then try this:

$('h1.h1class').click(function() { $(this).next('p.contents').slideToggle(500); });

Upvotes: 0

j08691
j08691

Reputation: 207901

$('h1').click(function(){
    $(this).next().slideToggle();
});

jsFiddle example

Upvotes: 2

Related Questions