Solved

Jquery: hide all <p> following <h1> down to next <h1> tag

Posted on 2011-03-09
6
843 Views
Last Modified: 2013-11-11
I am looking to use Jquery to collapse / hide some elements within a Wordpress blog post.

What I need is:

a series of <h1> and paragraph tags on a page.
<h1> tags to be clickable, and to toggle the <p> tags below it ONLY SO FAR AS THE NEXT <h1> tag

I know how to do this if all of the <p> tags were in a <div> directly below the <h1> but because it's not possible to put the <p> in a div, and there could be any number of <p> tags I'm getting stuck.

So, code will look like:

<h1>Title 1</h1>
<p>One</p>
<p>Two</p>
<p>Three</p>

<h1>Title 2</h1>
<p>One</p>

<h1>Title 3</h1>
<p>One</p>
<p>Two</p>

A tweaking of the attached code would be great
// Andy Langton's show/hide/mini-accordion - updated 23/11/2009
// Latest version @ http://andylangton.co.uk/jquery-show-hide

// this tells jquery to run the function below once the DOM is ready
$(document).ready(function() {

// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='Show';
var hideText='Hide';

// initialise the visibility check
var is_visible = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.toggle').prev().append(' (<a href="#" class="toggleLink">'+showText+'</a>)');

// hide all of the elements with a class of 'toggle'
$('.toggle').hide();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
is_visible = !is_visible;

// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);

// toggle the display - uncomment the next line for a basic "accordion" style
//$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().next('.toggle').toggle('slow');

// return false so any link destination is not followed
return false;

});
});

Open in new window

0
Comment
Question by:freestate
  • 3
  • 3
6 Comments
 
LVL 13

Expert Comment

by:darren-w-
ID: 35081997

        <script type="text/javascript">
            $(function(){
                $("h1").click(function(){
                    $(this).nextUntil("h1").hide();
                });
            });
        </script>

Open in new window

0
 
LVL 13

Accepted Solution

by:
darren-w- earned 500 total points
ID: 35082004
Tweaked code:

// Andy Langton's show/hide/mini-accordion - updated 23/11/2009
// Latest version @ http://andylangton.co.uk/jquery-show-hide

// this tells jquery to run the function below once the DOM is ready
//$(document).ready(function() {
 $(function(){ //use shortform of above

//new bit
               $("h1").click(function(){
                    $(this).nextUntil("h1").hide();
                });


// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='Show';
var hideText='Hide';

// initialise the visibility check
var is_visible = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.toggle').prev().append(' (<a href="#" class="toggleLink">'+showText+'</a>)');

// hide all of the elements with a class of 'toggle'
$('.toggle').hide();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
is_visible = !is_visible;

// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);

// toggle the display - uncomment the next line for a basic "accordion" style
//$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().next('.toggle').toggle('slow');

// return false so any link destination is not followed
return false;

});
});

Open in new window

0
 

Author Comment

by:freestate
ID: 35082109
Thanks for the reply Darren - I didn't know about .nextUntil

Will check that out.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:freestate
ID: 35082121
One quick question - how do I exclude the first element after the <h1> from the hide()? (For where there is an introductory paragraph.)
0
 

Author Comment

by:freestate
ID: 35082173
And also - how do I hide all the elements on page load?

$('.toggle').hide(); is obviously doing it within the code, but I can't add classes to the paragraphs - it needs to be all paragraphs following h1 tags that are hidden.

Thanks
0
 
LVL 13

Assisted Solution

by:darren-w-
darren-w- earned 500 total points
ID: 35082283
exclude first one (line 10): $(this).next().nextUntil("h1").hide()
hide at start(before line 8):     $("h1").nextUntil("h1").hide();
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now