0

Hello would someone help me out here?

I am trying to to do the following :

<table>
<thead> 
    <tr> 
        <th>Select</th> 
        <th>Name</th> 
    </tr>
</thead>    
<tr>
    <td>
        <input type="checkbox" class="toggle">
    </td>
    <td>
        Sam, Walls
    </td>
</tr>
<tr class="show">
    <td colspan="2">
        This is the info about Sam.
    </td>
</tr>
<tr>
    <td>
        <input type="checkbox" class="toggle">
    </td>
    <td>
        John, Doe
    </td>
</tr>
<tr class="show">
    <td colspan="2">
        This is the info about John.
    </td>
</tr>
</table>

When the checkbox with class="toggle" is clicked I only want the row below it to appear.

My JavaScript code that does not work so well is as follows:

$('.show').hide();
$('.toggle').click(function(){
    $('.show').slideToggle();
});

Currently it will only hide and toggle the Sam, Walls row.

EDIT : After using class instead of ID the hide issue seems to have been solved. Now however when I click one check box, both rows are toggled together. I updated the code above.

Any help would be appreciated!

Cheers.

2
  • 2
    You can't have two items with the same ID. Change it to a class `.toggle' Commented Feb 13, 2013 at 20:56
  • well that solved the hide issue. Now however both rows are toggled. Commented Feb 13, 2013 at 20:59

2 Answers 2

2

I mentioned in a comment that you cannot have duplicate id's, and that also goes for your #show element. Use the next function instead so whatever item you press, the next element will toggle. Something like:

$('.show').hide();
$('.toggle').click(function(){
    $(this).closest('tr').next().slideToggle();
});
Sign up to request clarification or add additional context in comments.

1 Comment

should be $(this).closest('tr').next().slideToggle(); and this fixed it, Thank you!
1

You can use your classes and toggle using the index of the one clicked...

$('.show').hide();
$('.toggle').click(function(){
    $('.show').eq($('.toggle').index(this)).slideToggle();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.