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

21 Effective Strategies to Amplify Website Traffic

The article provides a comprehensive guide to driving traffic to your website, highlighting 21 effective…

46 mins ago

Essentials for Ecommerce Website Development

While e-commerce is projected to account for more than $6.5 trillion in sales by 2023,…

13 hours ago

17 Essential Link Building Statistics and Trends for Enhanced SEO

The analysis highlights the significance of link-building in SEO, revealing that most websites neglect backlinks,…

1 day ago

Get a copy of the book Blog Blazers!

Hey everyone, I've got two copies of Blog Blazers that I want to give away…

2 days ago

101 of the best blogging tools in 2024,

101 of the best blogging tools in 2024. Having the best blogging tools continues to…

2 days ago

Remove the title attribute using jQuery

In WordPress, when you use wp_page_menu your anchor attribute's usually carry a title with the…

3 days ago