Connect with us

Tips & Tricks

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

In a previous post I talked about how to show/hide a single div html code with a search inside. Today I’d like to show you how I implemented jQuery into my new theme.

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.

Random Search Terms

Title

Top Articles

Top Searches

powered by WassUp

Recent Posts: Fully Net Worth . com

Albert Haynesworth Net Worth: How Rich is Albert Haynesworth?

Albert Haynesworth Net Worth: How Rich is Albert Haynesworth?

An American football player, Albert Haynesworth is known for his defensive tackle position. As of 2019, Albert Haynesworth net worth is estimated to be around $41 million. The 38 years old (June 17, 1981), Albert Haynesworth stepped in sports arena from his high school. In his high school, he took part in football, track and […]

Sandy Mahl Net Worth: How Rich is Garth Brooks’ ex-wife?

Sandy Mahl Net Worth: How Rich is Garth Brooks’ ex-wife?

Sandy Mahl is an American businesswoman. She is known to many as the ex-wife of country music star Garth Brooks. As of 2019, Sandy Mahl net worth is estimated to be $125 million. Sandy was born on January 16, 1965, in Tulsa, Oklahoma to parents John Mahl and Pat Mahl. Sandy is now a wildlife […]

Scott Fischer Net Worth

Scott Fischer Net Worth

Scott Fischer, depending on which one you are searching for. Instead, it is the famous hiker that died in 1996, or Scott Fischer, CEO of Dippin Dots, LLC, and Dippin Dots Cryogenics.  In this article we will be talking about the Dippin Dots CEO, Scott Fischer, who went on the hit show Undercover Boss we’re […]

%d bloggers like this: