Solved

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

Posted on 2014-01-03
11
183 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

708 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

17 Experts available now in Live!

Get 1:1 Help Now