Solved

How do you make an "over ride" collapsible div

Posted on 2013-12-09
7
188 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

734 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