Solved

How do you open two div's at once with one click

Posted on 2014-01-03
11
187 Views
Last Modified: 2014-01-16
Hi, I am really close thanks to you guys!!

I need to be able to open and close two div's at once based on one click.  I think I would have it if I could take out one of the "[+] Dates and Locations" on each row and have the remaining one open and close the two div's:

Here is what I have so far (thanks leakim971)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<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 () {
    $("#toggle_all_dates").on('click', function () {
        var isShowingAll = $(this).hasClass('showAll');
        var anchors = $("#dates_list li a");
        anchors[ isShowingAll ? "removeClass" : "addClass" ]("open");
        $(this).toggleClass('showAll');
        toggleDate(anchors, isShowingAll);
        $(this).html( isShowingAll ? 'Show all  Dates and Locations' : 'Hide all Dates and Locations' );
    });    
    
    /*
    $("#dates_list li a").on('click', function () {
        var mOpen = $(this).hasClass("open");
        $(this).toggleClass("open");
        toggleDate($(this), mOpen);        
    });
    */

    $("#dates_list li a").on('click', function () {
        var mOpen = $(this).hasClass("open");
        var anchors = $(this).closest("tr").find("li a");
        anchors[ mOpen ? "removeClass" : "addClass" ]("open");
        toggleDate(anchors, mOpen);        
    });
    
    function toggleDate($elem, isOpen){
        $elem.each(function() {
            $(this).html( isOpen ? '[+] Dates and Locations' : '[-] Dates and Locations');
            $(this).next()[ isOpen ? "slideUp" : "slideDown" ]();
        });
    }
});
</script>
<style>ul#dates_list{
    list-style-type: none;
    margin: 0;
    padding: 0;
}
ul#dates_list a{
    cursor: pointer;
}
ul#dates_list div.moredates {
    display: none;
} 
</style>
</head>

<body>
<a id="toggle_all_dates" class="moredates">Show all Dates and Locations</a>

<ul id="dates_list">
<table id="mytable">
<tr>
<td>
    <li>
        <a class="moredates">[+]Dates and Locations Row One</a>
        <div class="moredates" data-collapsed="true">More dates one...</div>
      
    </li>
    </td>
    <td>
    <li>
        <a>[+]Dates and Locations Row One</a>
        <div class="moredates" data-collapsed="true">More Locations two...</div> 
    </li>
    </td>
</tr>
<tr>
<td>
    <li>
        <a class="moredates">[+]Dates and Locations Row Two</a>
        <div class="moredates" data-collapsed="true">More dates one...</div>
      
    </li>
    </td>
    <td>
    <li>
        <a>[+]Dates and Locations Row Two</a>
        <div class="moredates" data-collapsed="true">More Locations two...</div> 
    </li>
    </td>
</tr>
</table>
</ul>
</body>
</html>

Open in new window

0
Comment
Question by:smfmetro10
  • 5
  • 5
11 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39755925
I don't see anything happening so far http://jsbin.com/EGoHObu/1/edit?html,output  Do you get something different?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39758524
That jsbin works for me Scott...Opens both divs?
0
 

Author Comment

by:smfmetro10
ID: 39771687
Yes!! Thanks!
Is it possible to only have one "[+] Dates and Locations" per row (but both div's open)?
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39775711
Hows that going to work?  Assume the numbers below are each [+] Dates and Locations .  If you click on 1 does is open 2?  what happens when you click on 2 does it open 3 or 1?

1
2
3
4
5
6
0
 

Author Comment

by:smfmetro10
ID: 39776960
I would like it to function the way it does now but with only one "[+] Dates and Locations" per row. Right now there are two of them but I can't figure out how to take one out and have it open both div's in the row
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39777976
Sorry I'm confused you want only one per row but open two on a row... Just not making any sense to me. What am I missing ?
0
 

Author Comment

by:smfmetro10
ID: 39778078
Sorry for not being able to better clarify.

Right now there are two "[+] Dates and Locations" per row. If you click either one of them, it opens both div's on the row. I would just like there to be only one place where you can click to open up both div's per row...

I attached some screen shots for better clarification.

Make sense?

Thanks for the help!
image1.png
image2.png
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39778159
Thanks for the explanation - it seems a lot clearer but rather than trying to force this to work, is it a possibility to restructure your html?

eg remove the table completely and use css to layout your content.
0
 

Author Comment

by:smfmetro10
ID: 39779668
No, The table is holding data from a database that you can click on the headers of the table to sort alphabetically and by date and by type.
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39784261
Ok,

I've just modified it slightly to exclude any anchors that are blank

http://jsbin.com/EGoHObu/4/edit?html,js,output

function toggleDate($elem, isOpen){
    $elem.each(function() {
      if ($(this).html() !== "") {
        $(this).html( isOpen ? '[+] Dates and Locations' : '[-] Dates and Locations');
      }
      $(this).next()[ isOpen ? "slideUp" : "slideDown" ]();
    });
  }

Open in new window

0
 

Author Closing Comment

by:smfmetro10
ID: 39786111
Great- Thank You!  The "Show all / Hide all dates and locations" is a bit clunky.

When you open the individual div's you have to cycle through the "Show all/Hide All..." to get it to be correct, but I can take it from here.

Thanks again!!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

777 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