Solved

How do you make an "over ride" collapsible div

Posted on 2013-12-09
7
184 Views
Last Modified: 2013-12-11
Hi,

I have some div's that toggle based on a click event... What I need is an "over ride" div to show all the div's at once. The trick is though that they can't just toggle.  For example if one div is open and the "show all dates" is clicked then the one div that is open has to remain open and the others will toggle open.

Here is what I have so far:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
    $(".moredates").click(function(){
        $(this).next().slideToggle();            
        $(this).html($(this).text() == 'Hide all dates' ? 'Show all dates' : 'Hide all dates');
    });
})
</script>
<script type="text/javascript">
$(document).ready(function(){
    $("#moredates_1").click(function(){
        $(this).next().slideToggle();            
        $(this).html($(this).text() == '[-] Dates' ? '[+] Dates' : '[-] Dates');
    });
})
</script>



<script type="text/javascript">
$(document).ready(function(){
    $("#moredates_2").click(function(){
        $(this).next().slideToggle();            
        $(this).html($(this).text() == '[-] Dates' ? '[+] Dates' : '[-] Dates');
    });
})
</script>

<script type="text/javascript">
$(document).ready(function(){
    $("#moredates_3").click(function(){
        $(this).next().slideToggle();            
        $(this).html($(this).text() == '[-] Dates' ? '[+] Dates' : '[-] Dates');
    });
})
</script>



<a id="switch" class="moredates">Show All Dates and Locations</a>
<div id="switch">
 <a id="moredates_1" >[+]Dates 1</a>
<div id="moredates_1" class="moredates" data-collapsed="true">
More dates one...
</div> 

 <a id="moredates_2" >[+]Dates</a>
<div id="moredates_2" class="moredates" data-collapsed="true">
More dates two...
</div>

 <a id="moredates_3" >[+]Dates</a>
<div id="moredates_3" class="moredates" data-collapsed="true">
More dates three...
</div>

</div>

Open in new window


Thanks for the help!
0
Comment
Question by:smfmetro10
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
Mate... this is wrong it several ways...

1st: you have more that one element with the same id.
Avoid this by all means...

2nd: when you collapse a date the next one breaks and appears in front of the previous

3rd: you're calling donument.ready for each event handler... why?
Use document.ready as less as possible.
Ideally if you really need to use it you should only have one document.ready per page.

4th: to fix your issue I'll rewrite all your code... ok? :)
0
 

Author Comment

by:smfmetro10
Comment Utility
Outstanding! Thanks!
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
Comment Utility
So here you go: http://jsfiddle.net/jV4fh/

This is far from perfect, but it's starting to look like something :)
I still kept some of your logic because it was faster but you get the idea.

For instance, to get the status of an element I never use the text, I use a class assigned to the element. It's easier to handle and in case you have a site that supports multiple languages you won't have any problem.

HTML
<a id="toggle_all_dates" class="moredates showAll">Show all dates</a>

<ul id="dates_list">
    <li>
        <a id="moredates_1">[+]Dates</a>
        <div class="moredates" data-collapsed="true">More dates one...</div> 
    </li>
    <li>
        <a id="moredates_2">[+]Dates</a>
        <div class="moredates" data-collapsed="true">More dates two...</div> 
    </li>
    <li>
        <a id="moredates_3">[+]Dates</a>
        <div class="moredates" data-collapsed="true">More dates three...</div>
    </li>
</ul>

Open in new window

CSS
ul#dates_list{
    list-style-type: none;
    margin: 0;
    padding: 0;
}
ul#dates_list a{
    cursor: pointer;
}
ul#dates_list div.moredates {
    display: none;
}

Open in new window

Javascript
$(document).ready(function () {
    $("#toggle_all_dates").on('click', function () {
        var $this = $(this);
        
        if($this.hasClass('showAll')){
            $('#dates_list li div.moredates')
                .not(':visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
        }else{
            $('#dates_list li div.moredates:visible')
                .prev()
                .each(function(idx,item){ toggleDate(item); });
        }
        
        $this.toggleClass('showAll');
        $(this).html($(this).text() == 'Hide all dates' ? 'Show all dates' : 'Hide all dates');
    });    
    
    $("#dates_list li a").on('click', function () {
        toggleDate(this);
    });
    
    function toggleDate(elem){
        var $elem = $(elem);
        $elem.next().slideToggle();
        $elem.html($elem.text() == '[-] Dates' ? '[+] Dates' : '[-] Dates');
    }
})

Open in new window

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
Bed time for me mate.
If you need me leave a comment, I'll be back in 7h more or less!

Cheers!
0
 

Author Comment

by:smfmetro10
Comment Utility
Thank you so much!!

 Is there a way to not have the <ul>?

The div's are actually in a loop based on some conditions
0
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
It's easier to manage lists like this.
If you're creating the div's in a loop then now create <li> element groups inside the <ul>
0
 

Author Closing Comment

by:smfmetro10
Comment Utility
Thanks!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now