Solved

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

Posted on 2014-01-03
11
191 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
[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
  • 5
  • 5
11 Comments
 
LVL 53

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 43

Expert Comment

by:Rob
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 43

Expert Comment

by:Rob
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 43

Expert Comment

by:Rob
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 43

Expert Comment

by:Rob
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 43

Accepted Solution

by:
Rob 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

SharePoint Admin?

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

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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