Solved

Trying to get jquery style accordian effect to work inside an html table

Posted on 2013-06-06
4
767 Views
Last Modified: 2013-06-10
I'm trying to get the jquery style accordian option to work inside an html table.  I have 2 header rows that won't change and then I have a dynamic html table that gets built. I have included some html code and javascript code I have as an example:

This is the output example
I would like to be able to hide/show first 2 rows after 'This is the header' is clicked and show/hide 3 rows after 'This is another header' is clicked

Main Header                              // static first header
header 1      header 2      header 3    // static second header
This is the header                      // first accordian header              
Research      Description      Partner
Research      Description      Partner
This is the another header         // second accordian header
Research_2      Description_2      Partner_2
Research_2      Description_2      Partner_2
Research_2      Description_2      Partner_2

<table class="research">
    <tbody>
        
        <tr><td colspan="3">Main Header</td></tr>
        <tr><td>header 1</td><td>header 2</td><td>header 3</td></tr>               
        
        <tr class="accordion">
            <td colspan="3">This is the header</td>
        </tr>
        <tr>
            <td>Research</td>
            <td>Description</td>
            <td>Partner</td>
        </tr>
        <tr>
            <td>Research</td>
            <td>Description</td>
            <td>Partner</td>
        </tr>  
        
        
        <tr class="accordion">
            <td colspan="3">This is another header</td>
        </tr>
        <tr>
            <td>Research_2</td>
            <td>Description_2</td>
            <td>Partner_2</td>
        </tr>
        <tr>
            <td>Research_2</td>
            <td>Description_2</td>
            <td>Partner_2</td>
        </tr>        
        <tr>
            <td>Research_2</td>
            <td>Description_2</td>
            <td>Partner_2</td>
        </tr>        

    </tbody>
</table>

$(function() {
    var $research = $('.research');
    $research.find("tr").not('.accordion').hide();
    $research.find("tr").eq(0).show();
    
    $research.find(".accordion").click(function(){
        $(this).siblings("tr").fadeToggle(500);
    }).eq(0).trigger('click');
});

Open in new window

0
Comment
Question by:dmalovich
  • 2
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Mrunal earned 500 total points
ID: 39228117
Here is your solution:

http://jsfiddle.net/PCpe4/
0
 

Author Closing Comment

by:dmalovich
ID: 39229019
Perfect. Thanks....
0
 

Author Comment

by:dmalovich
ID: 39229212
Is there any way to default the tabs to close when the page opens and have the user choose to open up a tab.  Sorry for asking after I closed the question.....
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 39233978
Yes this is possible:

just add below line in document.ready event

$('#table1 tr.accordion:eq(1)').nextUntil('tr.accordion').hide();

This will hide second content panel on page load.
you can change this index from 1 to 0 (zero).

Hope this helps you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does GoogleTagMgr javascripts below do 5 26
jQuery Dialog Autoresize Bug 2 24
Binding a Grid in Javascript 5 42
Running a javascript project in WebStorm 2 24
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…

790 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