Categories: Tips & Tricks

How to: show/hide a widget in WordPress with jQuery

As seen in the current theme, I am using jQuery to animate the show/hide or as known as the css style display: none;.

Since I am using a custom child theme on my site, and have Hybrid theme as my parent, the widgets or sidebar section is different than may be in your theme. But just apply the the style’s as follows to your theme.

First make sure that your WordPress site is calling jQuery, by plugging in this code into your header.php file above the <?php wp_head(); ?> text:

<?php wp_enqueue_script('jquery'); ?>

Then anywhere above the </head >, plug this code in:

<script type="text/javascript">
function toggleWidgets() {
 $('#primary h3.widget-title').addClass('plus');

 $('#primary h3.widget-title').click(function() {
  $(this).toggleClass('plus').toggleClass('minus').next().toggle(180);
 });

}
$(document).ready(function() {
 toggleWidgets();
}
</script>

That’s it. Pretty simple huh.

So lets go over what the code does.

$('#primary h3.widget-title').addClass('plus');

This line finds all <h3> tags with the class widget-title inside the ID parameter of #primary and adds a class of plus.

Then

$('#primary h3.widget-title').click(function() {
  $(this).toggleClass('plus').toggleClass('minus').next().toggle(180);
 });

Will apply a click function. When the H3 tag is clicked it will remove the class plus and add the class minus.

Then the code that says .next will then toggle the “next” element after the <h3> title.

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

View Comments

Recent Posts

WPLover: On Launching a WordPress theme

I just came across this article over at WPLover. Was very good, especially since I…

5 hours ago

Maintenance release for Hybrid 0.5

Today has released an update to his Hybrid Theme. Version 0.5.1 can be found at…

17 hours ago

Using the swekey on your blog

If you've downloaded the Swekey plugin and plan on using it for users to login…

1 day ago

April’s new meetup location for LAWPUG

Anyone in the Los Angeles area? Well the first Sunday of the month is the…

2 days ago

‘Portal’ And ‘Left 4 Dead 2’ Arrive On Linux

Steam is powerful, hot, and painful. It can move trains, boats, and has driven minds…

2 days ago

Swekey: A safe web enabler usb?

I received a Swekey in the mail this week. What's a Swekey you ask? Like…

3 days ago