SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.



    Code:
    <script>
    $(document).ready(function(){
      $(".content").on('click', function(){
    
      $(".info p").toggle();
      });
    });
    </script>
    HTML Code:
    <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>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hey RedBishop,

    You can do it like this:

    Code:
    <!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

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  4. #4
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

    Code:
    <script>
    $(document).ready(function(){
          $(".content").on('click', function(){
            $(this).next().toggle();
    	$(this).css("background-image", "url(image2.png)");				  
          });
    });
    </script>
    HTML Code:
    <div class="content" style="background: url(image1.png)">Menu</div>

    Thank you for your help.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    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.

  6. #6
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.




    HTML Code:
    <!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>

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hey there,

    I'd do it like this:

    Code:
    <!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.

  8. #8
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

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

  10. #10
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

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

    This is the code from earlier:

    Code:
    <!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>

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi RedBishop,

    This should do you:

    Code:
    <!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

  12. #12
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,055
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by RedBishop View Post
    I have been sick (fever etc..)
    That sucks! I hope you are feeling better now.

  14. #14
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm feeling a lot better thanks

    Hope you are having an enjoyable week.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •