Solved

Show and hide elements with JavaScript

Posted on 2016-10-07
8
60 Views
Last Modified: 2016-10-07
Hi Experts,
Please take a look at the attached code. My goal is to close any open row that shows the details when a different name is clicked.
Right now all colspan="3" rows can be opened simultaneously. How can the code be modified so that only one colspan="3" row can be open when a Name is clicked?
Thank you for your help.
<html>
<head>
<title>Toggle</title>
<script type="text/javascript" src="/jquery/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function() {
    $("td[colspan=3]").find("p").hide();
    $("table").click(function(event) {
        event.stopPropagation();
        var $target = $(event.target);
        if ( $target.closest("td").attr("colspan") > 1 ) {
            $target.slideUp();
        } else {
            $target.closest("tr").next().find("p").slideToggle();
        }                    
    });
});
</script>
</head>
<body>

Click on a row for more info:
<table border ="1">
    <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
    <tr><td colspan="3"><p>First Person Details</p>
    </td></tr>

    <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
    <tr><td colspan="3"><p>Second Person Details</p>
    </td></tr>
    
    <tr><td><p>Name</p></td><td><p>Age</p></td><td><p>Info</p></td></tr>
    <tr><td colspan="3"><p>Third Person Details</p>
    </td></tr>    
</table>

</body>
</html>

Open in new window

0
Comment
Question by:romsom
[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
  • 3
  • 2
  • 2
8 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41833974
colspan=3 is a column not a row. And it is pointless to use colspan=3 when that field in every row is colspan=3. colspan is like merging cells in Excel. It means that this cell should span the width of 3 cells in another row in this table. Perhaps you have another row that makes this useful though.

Are you wanting to hide just the colspan=3 cells and leave the other cells on that row visible. Otherwise, how would you click on the name if the whole row is hidden?

You cannot hide a table cell (<td>) without disrupting your table structure. However, since you've used <p> tags within your table, you could hide the <p> elements within those cells. This would be much easier in jQuery. Are you able to use jQuery?
0
 

Author Comment

by:romsom
ID: 41833986
Yes, I'm using jQuery.
I would like to hide the entire row that contains the Person Details when a different Name is clicked.
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41833988
Try this
$(function() {
  $('tr:odd p').hide();
  $('tr:even').click(function() {
    $('tr:odd p').hide();
    $(this).next().find('p').slideToggle();
  });
});

Open in new window

1
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:Kim Walker
ID: 41833997
I guess I need to slow down. I read those as three long rows of cells instead of 6 short rows. On my screen the first row ran the full width of the box so I thought it just word wrapped to the next line. I should know that code doesn't word wrap.

Anyway, Julian has provided the code you need.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41833998
Slight improvement - fixes problem of slideToggle on open item if clicked
$(function() {
  $('tr:odd p').hide();
  $('tr:even').click(function() {
    if (!$(this).next().find('p').is(':visible')) {
      $('tr:odd p').hide();
    }
    $(this).next().find('p').slideToggle();
  });
});

Open in new window

0
 

Author Closing Comment

by:romsom
ID: 41834005
Thank you very much, it's perfect.
0
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 41834091
You are welcome.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

738 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