A very simple jQuery accordion

This is a very simple script that hides and reveals the next element in the DOM when a level 2 heading is clicked. The script begins by hiding all paragraphs.

$(document).ready(function() {
	$('#accordion p').hide()
	$('#accordion h2').click(function() {
		$(this).next().slideToggle();
	});
});

This script functions perfectly. The only slight problem is that if you view the DOM in FireBug, you will notice that jQuery uses an inline style to hide the paragraphs. A better way would be to create a CSS rule to hide the paragraphs and then toggle that rule.

Button 1

Brownie applicake donut oat cake. Faworki cotton candy jelly gummies biscuit chupa chups. Ice cream jujubes tart ice cream sugar plum wafer.

Button 2

Tiramisu candy canes muffin tiramisu marshmallow muffin I love halvah toffee. Jujubes chocolate chocolate bar wafer topping. Croissant fruitcake croissant. Candy canes I love I love I love gummies oat cake.

Button 3

Chocolate bar macaroon chocolate bar tiramisu. Dessert I love candy canes cake powder halvah apple pie applicake. I love pie I love brownie I love.

Button 4

Liquorice croissant oat cake sesame snaps gingerbread oat cake macaroon gummies bear claw. Macaroon jelly-o croissant I love jujubes halvah. Pie cake jelly beans wafer. Chupa chups muffin sweet roll cake pudding.

By Course
Breakfast & Brunch
Starters
Salads
Main Dishes
Desserts
Beverages
By Main Ingredient
Meat
Pasta
Game & Poultry
Fish & Seafood
Vegetables/ Vegetarian
Cheese & Dairy
Breads
Sauces & Spices
Lifestyle
Regional Cuisine
Kids
BBQ & Outdoors
Everyday Cooking
Quick & Easy
Healthy
Seasonal
Christmas
New Years
Easter
Thanksgiving

Liquorice croissant oat cake sesame snaps gingerbread oat cake macaroon gummies bear claw. Macaroon jelly-o croissant I love jujubes halvah. Pie cake jelly beans wafer. Chupa chups muffin sweet roll cake pudding.