There's a few ways to do what you're doing.
The code you posted uses a javascript framework - basically a box of tools - called jQuery. Being familar with
jQuery selectors will make it easier for you to understand what's going on in the code and tweak it to behave how you want.
The problem that you're having is caused by the fact that your code is selecting ALL the paragraph elements. You don't want that: you just want to affect a particular paragraph.
One way to get around it is to give your buttons and your paragraphs
ID tags. This will let you tie a particular button to a corresponding paragraph:
Code:
<p id="paragraph_one">Hello World</p>
<button id="button_one">Toggle</button>
with that HTML, you can then tell specific elements what to do:
Code:
$("#button_one").click(function () {
$("#paragraph_one").slideToggle("slow");
});
(note the # symbol that tells jQuery to look for an element with that ID)
Now, that's fine - but you've probably noticed that if you've got more than one paragraph-and-button combination on your page, you're going to have to copy that javascript code for each one, like so:
Code:
$("#button_one").click(function () {
$("#paragraph_one").slideToggle("slow");
});
$("#button_two").click(function () {
$("#paragraph_two").slideToggle("slow");
});
$("#button_three").click(function () {
$("#paragraph_three").slideToggle("slow");
});
That's not great. It might be the best solution, depending on how your HTML is put together, but there might be another way: instead of telling the button explicitly which paragraph to open, make it work it out for itself.
There are a couple of ways to do this. One way is to use jQuery's
next() function to tell the button that it should be controlling the paragraph right next to it:
Code:
$("button").click(function () { // when any button is clicked
$(this).next("p").slideToggle("slow"); // find the next paragraph tag and toggle it
});
Another way is to work out the ID of the paragraph from the ID of the button. So a button called "button_one" will look for a paragraph called "paragraph_one", and a button called "intro_button" will look for a paragraph called "intro_paragraph".
Code:
$("button").click(function () {
var this_button_id = $(this).attr("id");
var target_paragraph_id = this_button_id.replace("button", "paragraph");
$("#" + target_paragraph_id).slideToggle("slow");
});
Each method has its own advantages and drawbacks - it depends how you've structured your HTML and how flexible you want your javascript to be.
Any help?