Jump to content

show hide issue


toolman

Recommended Posts

Hi,

 

I have some show/hide links like this:

 

<a href="javascript:ReverseDisplay('content1')"  class="hide-show-button">show</a>
<a href="javascript:ReverseDisplay('content2')"  class="hide-show-button">show</a>
<a href="javascript:ReverseDisplay('content3')"  class="hide-show-button">show</a>
<a href="javascript:ReverseDisplay('content4')"  class="hide-show-button">show</a>

 

using this code:

 

var click = 0;
$('.hide-show-button').click(function() {
     if(click == 1)
     {
        $(this).html('show');
        $('.hide-show').hide();
        click--;
     }
     else
     {
        $(this).html('hide');
        $('.hide-show').show();
        click++;
     }

});

 

If I click one link, it shows the content and then shows "hide" which is correct. However, if I click another link, it shows the content, but displays "show" until I click it again, so I end up in a strange loop.

 

Is there a way I can have each of the link always display "show" when not collapsed and "hide" when collapsed?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/291100-show-hide-issue/
Share on other sites

Instead of using the "click" variable, you could test the value of the clicked link. Perhaps the following will help:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(e) {
     $('.hide-show-button').click(function() {
          //UPDATE LINK LABEL
          if($(this).html() == 'show') {
               $(this).html('hide');
          } else {
               $(this).html('show');
          }
 
          //RESET OTHER LINK LABELS
          $(this).siblings().html('show');
     });
});
</script>
 
<a href="javascript:;"  class="hide-show-button">show</a>
<a href="javascript:;"  class="hide-show-button">show</a>
<a href="javascript:;"  class="hide-show-button">show</a>
<a href="javascript:;"  class="hide-show-button">show</a>

Note that the code was simplified so that I could produce a working example.

Link to comment
https://forums.phpfreaks.com/topic/291100-show-hide-issue/#findComment-1491289
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.