Results 1 to 4 of 4

Thread: [RESOLVED] Help with a JQuery script - selecting a p after a h1 and...

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Resolved [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.
    My Blog.

    Ryan Jones.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    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.

  4. #4

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    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.
    My Blog.

    Ryan Jones.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width