|
-
Dec 2nd, 2009, 04:58 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Help with a JQuery script - selecting a p after a h1 and...
Hi guys!
I've been trying to put together a JQuery script that does the following but so far I have had no success at all. If anyone can help out with this I'd be more appreciative.
I'm looking for a jQuery script that looks for all the h1's in a document who's next sibling is a p element. There can be multiple of these on a page like this:
HTML Code:
<h1>Blah</h1>
<p>
// Some content goes in here.
</p>
<h1>Blah2</h1>
<p>
// Some content goes in here.
</p>
Now. I'd like to make it so that when the h1 element is clicked it hides the p element with all of it's contents. I can't seem to figure out how to do this.
-
Dec 2nd, 2009, 11:55 AM
#2
Re: Help with a JQuery script - selecting a p after a h1 and...
while I can't directly help you, I have a suggestion if you could change the markup. it might be easier to just have a <div> containing the h1 and paragraph elements with a specific class name, or something.
-
Dec 2nd, 2009, 12:28 PM
#3
Re: Help with a JQuery script - selecting a p after a h1 and...
Code:
$("h1").click(function(){
$(this).next("p").hide();
});
Attaches a click event function to all <h1>, which uses the next() method, specified to find a <p> tag only, and hide it. If you need to toggle visibility instead, you'll just need some if/else logic in the function.
Tis one of the nice things about jQuery - rarely will you need to adjust your markup to accommodate it.
Last edited by SambaNeko; Dec 2nd, 2009 at 12:32 PM.
-
Dec 3rd, 2009, 07:38 AM
#4
Thread Starter
Frenzied Member
Re: Help with a JQuery script - selecting a p after a h1 and...
Thanks SambaNeko. That was just what I was looking for. What I has was more complex - guess simpler is often better.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|