8

How To Use jQuery To Make Your Content Slide Toggle

It's about time you started to learn something new right? Well here's your chance. jQuery is the newest and coolest thing when it comes to websites. Everyone is using it these days, and why not when it's so easy to do.In this tutorial you will learn just how easy it is by learning how to make your content animate by clicking on the headings....
How To Use jQuery To Make Your Content Slide Toggle

How to get started with jQuery

You can check out the demo of this tutorial to see what it does, or view the source.

First thing you need to do is go to jQuery.com and download the latest version of jQuery. The last release on January 14th was 1.4, and with that release The 14 Days of jQuery was also created to help people learn about what's new in 1.4. Upon the new release of jQuery, I also posted 14 Ways To Start Learning jQuery which I had originally posted as 13 ways and then realized jQuery 1.4 was released that day so I added The 14 Days to my list, and changed it to 14 ways. Coincidence? Yes, it was strange actually.

So now that you have downloaded the new jQuery library, and have a couple of resources for learning it, here's a quick crash course in how to start using it in a web page. I'll show you how to use the built-in slideToggle() function to make your H2 headings toggle when you click them. This could be used for example, to make your sidebar widgets toggle.

First you need to include jQuery in the head of your page.

<script src="jquery-1.4.js" type="text/javascript"></script>

Now for the fun part. The following code can go below the jQuery include code in the head of your page, as shown above. Keep in mind this is the recommended way, though you can put all of this in the body of your page as well.


$(document).ready(function(){
	$('h2').next().hide();

	$('h2').click(function () {
		$(this).next().slideToggle('slow');
	});

}); 

Let's break this code down.
The line $(document).ready(function(){ is what you need to make jQuery work. Everything inside it will load as soon as the DOM is loaded, before the page contents are loaded. You can also write it like this instead $(function() { or jQuery(document).ready(function(){. They all work the same way.

The next line $('h2').next('div').hide(); will find all h2 headings, select whatever is following it .next() and hide it .hide(). This could be a list for example if that's what is after your h2. You could be even more specific if you wanted and select only the h2's that have "div" after them by writing .next('div').

Now to make the headings clickable you need to write $('h2').click(function () {, and then to show what we have hidden you write $(this).next().slideToggle();. If you specified .next('div') you need to do that again here. I included slow in my slideToggle() function which will make it slide slowly. You can also use fast, or a number in miliseconds for example .slideToggle(5000) for 5 seconds.

Finally we need to close both the functions with });

This is what I mean when I say jQuery is easy

This is just the first of many more jQuery tutorials you will find here on New2WP. I've only just begun to learn it, and I'm already in love. It's the perfect thing for designers who want to learn programming but have a hard time with it.

If you have any questions let me know below.

UPDATE: A better and shorter code that shows the cursor when hovering over H2

<script type="text/javascript">
$(function($) {
	$('h2').css({'cursor':'pointer'}).next().hide().prev().click(function () {
		$(this).next().slideToggle();
	});
});
</script>

This does performs the same task only in less code, and with the pointer cursor provided by Samuel.

Shortlink:
Share This Post

Get automatic updates! Subscribe to Our RSS Feed or Get Email Updates sent straight to your inbox!

About the Author

Jared is from Boston working as a web and graphic designer. Also owns the design blog Tweeaks.com, and has designed many other websites powered by Wordpress including the New2WP theme.

Level: Noob

User Comments

( ADD YOURS )
  1. Samuel February 3


    Shorter code and pointer cursor on header:

    jQuery(function($) {
    $('h2').css({'cursor':'pointer'}).next().hide().prev().click(function () {
    $(this).next().slideToggle();
    });
    });


  2. Cool thanks. I was trying to figure out how to get the cursor to show.


  3. Jared, do know what to do if conflict appears? I've read about it on jQuery site, done what they've said - put noConflict and changed $ for $j and no effects... I've got prototype and easyslider on my wordpress site - i suppose they conflict... but what should i do? This is my web site:
    http://2grow.pl/informacje/formalnie/test
    where i've tried to do "halo" as a slide toggle
    I would be very grateful for help


  4. Hmm, have you tried changing all of the $ to $$$ in prototype.js?


  5. I also saw that you have an older version of the easySlider plugin that you are using. Alen has an updated easySlider plugin 1.7 you can find here

    http://cssglobe.com/post/5780/easy-slider-17-numeric-navigation-jquery-slider

    Let me know if the previous suggestion works or not too.


  6. Jared, thanks very much for help, before i've read your answer I've found a soilution for wordpress - there's no need to fight in jQuerry as there's a plugin called collapsible elements. If somebody wants to see how it works:
    http://4grow.pl/onas/zespol/mateusz-dabrowski/
    It hasn't got advanced options as time of collapsing - just one button that is added to edit field. Very simple and quick:)

    And one more thing - i haven't managed to read your answer, as i forgot about it for a certain time. But certainly if u had had a plugin called "subscribe to comments", I would have ticked "let me know about new comment" and I wouldn't have been able to forget...:) Try it out!


  7. I'm glad you found a solution.

    And I have re-enabled the subscribe to comments plugin, which I need to hack a little bit. There is a checkbox below the comment form that you can check to subscribe to comments. I need to make that more apparent though.


  8. i don't have any idea about jquery; Your blog helps me much; Thanks for your blog;

Trackbacks


    1. Avatar

      Your Name
      September 8


      CommentLuv Enabled