Help needed with regards to jQuery toggle

Hi everyone,

I’m trying to use jQuery to show and hide content using the toggle method. This shouldn’t be that difficult to do but…

When I click a specific div all of the divs are toggled. Only the content of the specific div that I click needs to be toggled.

Can someone please help me out?

Thank you in advance.

<script>
$(document).ready(function(){
  $(".content").on('click', function(){

  $(".info p").toggle();
  });
});
</script>
<div class="content">Menu</div>
<div class="info">
<p>hi there</p>
</div>

<div class="content">Menu</div>
<div class="info">
<p>hi there2</p>
</div>

<div class="content">Menu</div>
<div class="info">
<p>hi there3</p>
</div>

Hey RedBishop,

You can do it like this:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Toggle example</title>
    <style>
      .info{ display: none; }
    </style>
  </head>
  
  <body>
    <div class="content">Menu</div>
    <div class="info">
      <p>hi there</p>
    </div>

    <div class="content">Menu</div>
    <div class="info">
      <p>hi there2</p>
    </div>

    <div class="content">Menu</div>
    <div class="info">
      <p>hi there3</p>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>   
    <script>
      $(".content").on('click', function(){
        $(this).next().toggle();
      });
    </script>
  </body>
</html>

HTH

Hi Pullo,

how are you doing?

Thanks a lot - it is working fine now. It would have taken me some time to figure out.

Enjoy the weekend.

Hi Pullo,

I’m hoping you could please assist me with this.

I’m displaying an image next to the menu text. When the div is clicked and the hidden content is revealed, I’d like to swop the image for another image. Then when the menu is closed the previous image is again displayed. I have played around with the code but have not been able to achieve the toggling effect with the images.

I’ve used the code below but it only changes the image once.

<script>
$(document).ready(function(){
      $(".content").on('click', function(){
        $(this).next().toggle();
	$(this).css("background-image", "url(image2.png)");				
      });
});
</script>

<div class="content" style="background: url(image1.png)">Menu</div>

Thank you for your help.

Hey RedBishop,

Could you provide a little more HTML?
(Kind of like what I did in post#2, so that I can copy it and run it on my PC)

I’m sure this won’t be hard to figure out, but I just need to see what you’re working with right now.

Thank you for getting back to me!

I don’t really have more HTML that I could show you; hopefully what I’ve posted below is sufficient. The image next to the menu text will be a small icon, perhaps a downward-facing arrow that changes to an upwards-facing arrow when the hidden content is shown.

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Toggle example</title>
    <style>
      .info{ display: none; }
    </style>
  </head>

  <body>
    <div class="content" style="background: #fff url(arrow1.png) 15% 55% no-repeat">Menu</div>
    <div class="info">
      <p>hi there</p>
    </div>

    <div class="content" style="background: #fff url(arrow1.png) 15% 55% no-repeat">Menu</div>
    <div class="info">
      <p>hi there2</p>
    </div>




<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
      $(".content").on('click', function(){
        $(this).next().toggle();
	$(this).css("background-image", "url(arrow2.png)");				
      });
});
</script>
  </body>
</html>

Hey there,

I’d do it like this:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Toggle example</title>
    <style>
      .info{ display: none; }
      .content{ border: solid gray 1px; padding: 5px; margin: 5px; width: 120px; }
      .closed{ background: #fff url("down_arrow.gif") 95% 45% no-repeat;}
      .open{ background: #fff url("up-arrow.gif") 95% 45% no-repeat;}
    </style>
  </head>
  
  <body>
    <div class="content closed">Menu item</div>
    <div class="info">
      <p>hi there</p>
    </div>

    <div class="content closed">Menu item</div>
    <div class="info">
      <p>hi there2</p>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>   
    <script>
      $(".content").on('click', function(){
        $(this).next().toggle();
        if($(this).hasClass("closed")){
          $(this).removeClass("closed");
          $(this).addClass("open");
        } else {
          $(this).removeClass("open");
          $(this).addClass("closed");
        }
      });
    </script>
  </body>
</html>

A couple of tips:

You hardly ever need to wrap things in a $(document).ready(){…} block.
Also, inline CSS and event handlers are bad. Use classes / unobtrusive JS instead.

Hi Pullo,

thank you! thank you! thank you!

It’s working perfectly. I sure appreciate your help. I now see that it was a matter of adding and removing classes instead of just changing the background image once. Very nice indeed.

And thanks for the tips - normally my CSS does reside in an external file.

Cheers

Hi there,

No problem :slight_smile:
I’m glad it’s working for you.
Have a good weekend (and nice Christmas for that matter).

Hi Pullo,

how are you doing?

I hope you will be able to help me. I’ve added a paragraph with an ID “toggle”. Now if this paragraph is clicked all of the divs should expand or close as the case may be.

This is my attempt but it doesn’t work completely. I think there is some mix up when some divs are closed and the toggle paragraph is then clicked to close all divs. What can I do to fix this?

Thank you in advance.

<script type="text/javascript">
$("#toggle").on('click', function(){
$(".content").next().toggle();
$(".content").removeClass("open");
$(".content").addClass("closed");
});
</script>

This is the code from earlier:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Toggle example</title>
    <style>
      .info{ display: none; }
      .content{ border: solid gray 1px; padding: 5px; margin: 5px; width: 120px; }
      .closed{ background: #fff url("down_arrow.gif") 95% 45% no-repeat;}
      .open{ background: #fff url("up-arrow.gif") 95% 45% no-repeat;}
    </style>
  </head>

  <body><p id="toggle">Toggle</p>
    <div class="content closed">Menu item</div>
    <div class="info">
      <p>hi there</p>
    </div>

    <div class="content closed">Menu item</div>
    <div class="info">
      <p>hi there2</p>
    </div>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>
      $(".content").on('click', function(){
        $(this).next().toggle();
        if($(this).hasClass("closed")){
          $(this).removeClass("closed");
          $(this).addClass("open");
        } else {
          $(this).removeClass("open");
          $(this).addClass("closed");
        }
      });
    </script>
  </body>
</html>

Hi RedBishop,

This should do you:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Toggle example</title>
    <style>
      .info{ display: none; }
      .content{ border: solid gray 1px; padding: 5px; margin: 5px; width: 120px; }
      .closed{ background: #fff url("down_arrow.gif") 95% 45% no-repeat;}
      .open{ background: #fff url("up-arrow.gif") 95% 45% no-repeat;}
      button{ margin: 5px; padding: 5px 15px; }
    </style>
  </head>
  
  <body>
    <div class="content closed">Menu item</div>
    <div class="info">
      <p>hi there</p>
    </div>

    <div class="content closed">Menu item</div>
    <div class="info">
      <p>hi there2</p>
    </div>

    <button id="toggle">Toggle all</button>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>   
    <script>
      $(".content").on('click', function(){
        $(this).next().toggle();
        if($(this).hasClass("closed")){
          $(this).removeClass("closed");
          $(this).addClass("open");
        } else {
          $(this).removeClass("open");
          $(this).addClass("closed");
        }
      });

      $("#toggle").on("click", function(){
        if($(".open").length){
          $(".info").each(function(){
            $(this).hide().removeClass("open").addClass("closed");
          });
        } else {
          $(".info").each(function(){
            $(this).show().removeClass("closed").addClass("open");
          });
        }
      });
    </script>
  </body>
</html>

The open/close logic has a bit of duplication and should probably be cleaned up.
I’ll leave that to you though, as I’m in a bit of a rush today :slight_smile:

Hi Pullo,

how are you doing?

Please forgive my late reply - I have been sick (fever etc…)

Thank you for posting that code, I’ll try it out and will get back to you.

Regards.

That sucks! I hope you are feeling better now.

I’m feeling a lot better thanks :slight_smile:

Hope you are having an enjoyable week.