Jump to content

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.