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.

9 Comments

9 Comments

  1. Rolograaf

    July 29, 2009 at 5:59 am

    Tried this, saw the javascript in the source of my blog, but the plus class was not added and click function not activated. Any idears what I could have done wrong?

    • Alik

      September 14, 2009 at 7:27 am

      Same problem here. In Firebug th script is there, but the class is not added to h3 widget class inside div ID primary.

      Any ideas?

  2. Pingback: 30 Tutorials Combining Both Wordpress and jQuery : Speckyboy Design Magazine

  3. Pingback: 30 Tutorials Combining Both Wordpress and jQuery | Coyot [at] CanalCoffee.com : WordPress

  4. Pingback: 30 tutorial para utilizar jQuery en nuestro Wordpress | code

  5. Pingback: 20+ jQuery and Wordpress Tutorials – A Match Made in Heaven

You must be logged in to post a comment Login

Random Search Terms

Title

Top Articles

powered by WassUp

Recent Posts: Fully Net Worth . com

Harry Hains Net Worth: How Rich was Jane Badler’s Son Actually?

Harry Hains Net Worth: How Rich was Jane Badler’s Son Actually?

Harry Hains was an Australian actor known to many for playing on American Horror Story. He is also the son of famous actress Jane Badler. Harry Hains net worth before his death is estimated to be $700,000. Harry was born on December 4, 1992, in Melbourne, Australia to Stephen Hains and Jane Badler. He had […]

Gabby Morrison Height, Age, Net Worth, Wiki, Bio & More

Gabby Morrison Height, Age, Net Worth, Wiki, Bio & More

Gabby Morrison is a young American social media star who is best known for creating videos on the popular and trendy platform TikTok. In a short amount of time, Morrison has been able to garner over 2.1 million followers on the platform where she has received close to 70 million likes for her videos already. […]

Tony Lopez Height, Age, Bio, Net Worth, Wiki & More

Tony Lopez Height, Age, Bio, Net Worth, Wiki & More

Tony Lopez is an American Tik Tok star who is best known for his skills as a dancer and along with his older brother, Ondreaz Lopez completes the synergy of The Lopez Brothers. He has over 2.4 million followers on his profile on Tik Tok where he has received more than 40 million likes so […]

%d bloggers like this: