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.

Recent Posts

Jon Asked: How to add thumbnails above post on the THiCK theme

I was asked by a reader: I'm using the THiCK theme for my new design…

4 hours ago

13 Best WordPress Email Marketing Plugins

The article lists the 13 best WordPress email marketing plugins for 2022 - HubSpot, ConvertKit,…

16 hours ago

AI Chips in 2024: Navigating NVIDIA’s Dominance and Emerging Competitors

In 2024, NVIDIA remains a dominant force in the AI chip market with its innovative…

1 day ago

21 Best ChatGPT Prompts for Marketing and How to Utilize Them Effectively

In the rapidly evolving realm of digital marketing, ChatGPT has emerged as an invaluable tool,…

2 days ago

WP Cult attending WordCamp Denver 2009

It is official, today I purchased my ticket for WordCamp Denver! I as well as…

2 days ago

Navigating the AI Revolution: 7 Key Challenges for Web Professionals

AI's Transformative Impact on Web Professionals Artificial intelligence (AI) is revolutionizing the digital landscape, and…

3 days ago