Solved

How do you make an "over ride" collapsible div

Posted on 2013-12-09
7
189 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39707330
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
ID: 39707403
Outstanding! Thanks!
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 39707410
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39707413
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
ID: 39709355
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
ID: 39710014
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
ID: 39711922
Thanks!
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

630 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