Joelle Nader
Joelle Nader

Reputation: 41

Making a div appear and disappear

I have 8 thumbnail pictures and I want to make a slider appear when I click on one of them. I tried pretty much everything I know and here it is:

HTML:

<div id="divThumbnails2" class="thumbnails2">
    <a href="#" id="Thumb5" >
        <img src="images/thumbnail_5.png" width="55" height="66" alt="" class="floatleft" /></a>
    <a href="#" id="Thumb6">
        <img src="images/thumbnail_6.png" width="56" height="67" alt="" class="floatleft"
                style="margin-left: 7px;" /></a>
    <a href="#" id="Thumb7">
        <img src="images/thumbnail_7.png" width="54" height="66" alt="" class="floatleft"
                style="margin-left: 7px;" /></a>
    <a href="#" id="Thumb8">
        <img src="images/thumbnail_8.png" width="57" height="68" alt="" class="floatleft"
                style="margin-left: 7px;" /></a>
</div>
<div id="divThumbnails" class="thumbnails">
    <div id="Thumb1" class="floatleft" >
        <a href="#">
            <img src="images/thumbnail_1.png" width="54" height="65" alt=""/></a>
    </div>
    <div id="Thumb2" class="floatleft" style="margin-left: 7px;" >
        <a href="#">
            <img src="images/thumbnail_2.png" width="56" height="66" alt="" /></a>
    </div>
    <div id="Thumb3" class="floatleft" style="margin-left: 7px;" >
        <a href="#">
            <img src="images/thumbnail_3.png" width="54" height="66" alt="" /></a>
    </div>
    <div class="floatleft" style="margin-left: 7px;" id="Thumb4">
        <a href="#">
            <img src="images/thumbnail_4.png" width="57" height="68" alt=""  /></a>
    </div> 
</div>

And the slides:

<div id="slides">
    <div class="slides_container" id="SlidesContainer">
        <a href="#"><img src="images/slides_1.png" width="408" height="266" alt="" /></a>
        <a href="#"><img src="images/slides_2.png" width="408" height="272" alt="" /></a>
        <a href="#"><img src="images/slides_3.png" width="408" height="275" alt="" /></a>
    </div>
    <a class="prev" href="#"><img width="28" height="27" alt="Arrow Prev" src="images/slides_left_arrow.png"/></a>
    <a class="next" href="#"><img width="28" height="27" alt="Arrow Next" src="images/slide_arrow_right.png"/></a>
    <div class="close_btn" id="Btn_Close"><a href="#"><img src="images/slide_close_btn.png" width="28" height="27" alt="Close" /></a></div>
</div>

The JavaScript:

<script type="text/javascript">

     
    var close = document.getElementById('Btn_Close');
    var slides = document.getElementById('slides');
    close.onclick = function () {

        slides.style.display = "none";
    };
</script>
<script type="text/javascript">

    var thumb1 = document.getElementById('Thumb1');
    var slides = document.getElementById('slides');
    thumb1.onclick = function () {
            
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb2 = document.getElementById('Thumb2');
    var slides = document.getElementById('slides');
    thumb2.onclick = function () {
           
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb3 = document.getElementById('Thumb3');
    var slides = document.getElementById('slides');
    thumb3.onclick = function () {
          
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb4 = document.getElementById('Thumb4');
    var slides = document.getElementById('slides');
    thumb4.onclick = function () {
           
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb5 = document.getElementById('Thumb5');
    var slides = document.getElementById('slides');
    thumb5.onclick = function () {
        
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb6 = document.getElementById('Thumb6');
    var slides = document.getElementById('slides');
    thumb6.onclick = function () {
        
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb7 = document.getElementById('Thumb7');
    var slides = document.getElementById('slides');
    thumb7.onclick = function () {
        
        slides.style.display = "block";
    };
</script>
<script type="text/javascript">
    var thumb8 = document.getElementById('Thumb8');
    var slides = document.getElementById('slides');
    thumb8.onclick = function () {
        
        slides.style.display = "block";
    };

</script>

Everything's fine with the slides_container when I click my arrows it changes my picture but I have to initially set its display property to "block" and I want to set it to 'none' because I want it to appear only when I click on one of the thumbnail pictures.

It works with the close button when I click on it the slides_container div disappears (I guess I'm doing something right) but I can't seem to get it to appear when I click on the others. Any help is appreciated.

Upvotes: 4

Views: 22611

Answers (2)

polyphemus11
polyphemus11

Reputation: 113

This function will set your display property to the opposite of what it currently is (on vs off). Change 'toggle' to your element id that you want to toggle on/off.

<script type="text/javascript">
    function toggle(){
        var off=document.getElementById('toggle');
        if (off.style.display == "none") {
            off.style.display = "block";
        } else {
            off.style.display = "none";
        }        
    }
</script>

Lastly, if you want to toggle more elements, you can always create additional variables. If you want more "switches", then create another element with another onclick action, pointing to another function that toggles the additional element id. Hope that helps.

Upvotes: 3

Arun P Johny
Arun P Johny

Reputation: 388436

Try

<div id="slides" style="display: none"> <!-- set display to none -->
    <div class="slides_container" id="SlidesContainer">
        <a href="#"><img src="images/slides_1.png" width="408" height="266" alt="" /></a>
        <a href="#"><img src="images/slides_2.png" width="408" height="272" alt="" /></a>
        <a href="#"><img src="images/slides_3.png" width="408" height="275" alt="" /></a>
    </div>
    <a class="prev" href="#"><img width="28" height="27" alt="Arrow Prev" src="images/slides_left_arrow.png"/></a>
    <a class="next" href="#"><img width="28" height="27" alt="Arrow Next" src="images/slide_arrow_right.png"/></a>
    <div class="close_btn" id="Btn_Close"><a href="#"><img src="images/slide_close_btn.png" width="28" height="27" alt="Close" /></a></div>
</div>

Update You can simplify the script a lot

<script type="text/javascript">
    var slides = document.getElementById('slides');

    var close = document.getElementById('Btn_Close');
    close.onclick = function () {
        slides.style.display = "none";
    };

    function showSlide(){
        slides.style.display = "block";
    }

Add an onclick event to the thumb elements:

<div id="Thumb1" class="floatleft" onclick="showSlide()">
    <a href="#">
        <img src="images/thumbnail_1.png" width="54" height="65" alt=""/></a>
</div>

Upvotes: 1

Related Questions