Categories: News

How to: Show/Hide any div box with jQuery in WordPress

If you take a look at my current sidebar (right) and see the heading Google Search you’ll notice that when you click it the Google Search box show’s into view.

Let me show you how this is done.

First make sure that your WordPress site is calling jQuery, buy pluggin this code into our 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">
jQuery(document).ready(function() {
 // hides the slickbox as soon as the DOM is ready
  jQuery('#toggle-search').hide();

 // toggles the slickbox on clicking the noted link
  jQuery('a#slick-slidetoggle').click(function() {
 jQuery('#toggle-search').slideToggle(400);
 return false;
  });
});
</script>

That’s simple, huh. Okay now lets write the Search code:

<h2><a href="#" id="slick-slidetoggle">Google Search</a></h2>
 <div id="toggle-search" style="padding:10px;">
    <form method="get" id="search" action="<?php bloginfo('home'); ?>/">
        <div>
            <input type="text" value="Enter Keyword" >

That’s it.

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

    • I agree, this is a great little trick and will be implementing it on one of my blogs pretty quick here.
      Thanks!

    • There sure is Jimmy,
      All you need to do is reuse the code, and change the #id to the new div box #id.

      since we already have:
      jQuery(document).ready(function() {
      // hides the slickbox as soon as the DOM is ready
      jQuery('#toggle-search').hide();

      // toggles the slickbox on clicking the noted link
      jQuery('a#slick-slidetoggle').click(function() {
      jQuery('#toggle-search').slideToggle(400);
      return false;
      });
      });

      We just need to add in:

      jQuery('#toggle-search').hide();
      jQuery('#toggle-tags').hide(); //option to hide the ID before the page loads

      And:

      jQuery('a#slick-slidetoggle').click(function() {
      jQuery('#toggle-search').slideToggle(400);
      return false;
      jQuery('a#slick-slidetoggle-tag').click(function() {
      jQuery('#toggle-tags').slideToggle(400);
      return false;

      That's it for the function. Just make sure you create a div box with the id of #toggle-tags

      • You missed out the " }); " . Thought I might point it out to save others from some frustration. Thanks for pointing out how to add multiple boxes.
        :)

        jQuery(document).ready(function() {
         // hides the slickbox as soon as the DOM is ready
         jQuery('#toggle-topic1').hide();
         jQuery('#toggle-topic2').hide();
        
         // toggles the slickbox on clicking the noted link
         jQuery('a#slick-slidetoggle1').click(function() {
         jQuery('#toggle-topic1').slideToggle(400);
         return false;
         });
         jQuery('a#slick-slidetoggle2').click(function() {
         jQuery('#toggle-topic2').slideToggle(400);
         return false;
         });
        });
        
  • Thanks a lot for that,
    but there something confusing me, i set it up like u said exactly and it works very fine, but it's not working inside the categories in wordpress when im using the permalinks?
    do you know why is that?

  • Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I'v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  • This is exactly what I was looking for, thanks!

    One question though. Is it possible to hide the box permanently by maybe setting a cookie, or checking if user is logged in?

1 2 3 4

Recent Posts

21 Effective Strategies to Amplify Website Traffic

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

1 hour ago

Essentials for Ecommerce Website Development

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

11 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,…

23 hours 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…

1 day 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…

2 days ago