Search code examples
javascriptjqueryhtmlcssshow-hide

I would like my Jquery script of show/hide to always display one of the hiddent conent and never display more then 1 hiddent text at once


I would like this Show/hide script to show the first hidden text on startup and at all times only show max one hidden content. So if im displaying the hidden content of text 3 and before i was viewing the hidden content of text 1 i know want the text 1 to slide back up if i press text 3, all in same .slidetoggle i have now.

$(document).ready(function() {
            $(".neverseen img").click(function() {
                $(this).parent().toggleClass("active");
                $(".neverseen p").slideToggle("slow");
                return false;
            });
        });
        $(document).ready(function() {
            $(".neverseen1 img").click(function() {
                $(this).parent().toggleClass("active");
                $(".neverseen1 p").slideToggle("slow");
                return false;
            });
        });
        $(document).ready(function() {
            $(".neverseen2 img").click(function() {
                $(this).parent().toggleClass("active");
                $(".neverseen2 p").slideToggle("slow");
                return false;
            });
        });
        $(document).ready(function() {
            $(".neverseen3 img").click(function() {
                $(this).parent().toggleClass("active");
                $(".neverseen3 p").slideToggle("slow");
                return false;
            });
        });
#leftpanel h1 {font-size: 18px; font-family: 'Montserrat bold'; color:#b0a887; border-top: 1px solid #b0a887; font-style: normal;}
#leftpanel p {display: none; color: #333; font-size: 14px;}
#leftpanel a {margin-left: 230px;}
#leftpanel img {}

.secondImage{
  display: none;
}

.active .firstImg{
  display:none;
}

.active .secondImage {
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="leftpanel">
                <div class="neverseen">
                    <h1>First title</h1>
                    <a href="#" id="show">
                        <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
                        <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
                    </a>
                    <p>First text</p>
                </div>
                <div class="neverseen1">
                    <h1>Second title</h1>
                    <a href="#" id="show1">
                        <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
                        <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
                    </a>
                    <p>Second text</p>
                </div>
                <div class="neverseen2">
                    <h1>Third title</h1>
                    <a href="#" id="show2">
                        <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
                        <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
                    </a>
                    <p>Third text</p>
                </div>
                <div class="neverseen3">
                    <h1>Last title</h1>
                    <a href="#" id="show3">
                        <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
                        <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
                    </a>
                    <p>Last text</p>
                </div>
            </div>


Solution

  • As suggested in another answer, you can use accordion but if you don't want to change your approach you do following,

    add sec class to neverseen{1/2/3} div's like

    <div id="leftpanel">
      <div class="sec neverseen">
        <h1>First title</h1>
        <a href="#" id="show" class='active'>
            <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
            <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
        </a>
        <p class='active'>First text</p>
    </div>
    <div class="sec neverseen1">
        <h1>Second title</h1>
        <a href="#" id="show1">
            <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
            <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
        </a>
        <p>Second text</p>
    </div>
    <div class="sec neverseen2">
        <h1>Third title</h1>
        <a href="#" id="show2">
            <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
            <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
        </a>
        <p>Third text</p>
    </div>
    <div class="sec neverseen3">
        <h1>Last title</h1>
        <a href="#" id="show3">
            <img class="secondImage" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-down-01-128.png" width="40" height="40">
            <img class="firstImg" src="https://cdn3.iconfinder.com/data/icons/faticons/32/arrow-up-01-128.png" width="40" height="40">
        </a>
        <p>Last text</p>
    </div>
    

    Now we don't have to add events for individual sections, we can add it on sec class

    $(".sec img").click(function() {
        var isActive = $(this).parent().hasClass('active');
        $('.sec a').removeClass('active');
        $('.sec p').hide('slideUp');
        if(!isActive) {
          $(this).parent().toggleClass("active"); 
          $(this).parent().next("p").slideToggle("slow");
        }
    
        return false;
    });
    

    Add this CSS at the end,

    #leftpanel p.active{
      display: block;  
    }
    

    Example

    What I am doing here is that I am attaching event to all images under sec div.

    When someone click on that, I am setting active class on parent div and resetting every other div.