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

A simple way to query posts

Here is a simple way to call query_posts with an array of options. For all…

5 hours ago

Adding a favicon to your site

Looking to add a favicon to you site? Inside your WordPress theme's functions file (functions.php)…

17 hours ago

The launch of WordPress 2.8

Wow, have you heard? WordPress has announced that the newest version, 2.8 which was thought…

1 day ago

WPLover: On Launching a WordPress theme

I just came across this article over at WPLover. Was very good, especially since I…

2 days ago

Maintenance release for Hybrid 0.5

Today has released an update to his Hybrid Theme. Version 0.5.1 can be found at…

2 days ago

Using the swekey on your blog

If you've downloaded the Swekey plugin and plan on using it for users to login…

3 days ago