View Full Version : Expand/Collapse using jQuery

Nov 6th, 2008, 04:57 PM
I'm implementing an expand/collapse feature but I have a problem:

I have 2 rows in a table...

<tr id="40807386" class="hidden ">testing this</tr>
<tr id="34893485" class="hidden ">testing this again</tr>

So I can hide this row by going
$('.hidden').toggle(); but this hides both the rows when I only want to hide the first row, not both. Is there a way to get the id of the element so that I can go something like
($(this).id).toggle() This ^ gives me an error but I need to be able to do something similar. Any help on this would be greatly appreciated


Nov 7th, 2008, 11:03 AM
You could select the elements based on id, see http://docs.jquery.com/Selectors/id#id .But the starting letter of an id value can't be a number. So your markup is invalid now.

Nov 7th, 2008, 08:44 PM
You can also try using these:

.eq(0) - Selects the first item
:first - Selects the first item

There's other ways also. Checkout the documentation on jquery.com for details/examples.

Nov 7th, 2008, 11:05 PM
but the problem is that I don't know the value of the id, so I need to somehow be able to retrieve the id of the current element $(this)

to do $('#id') I need to know what the id is...

EDIT: nvm guys, I figured it out. Thanks for the help tho, I appreciate it very much

Nov 8th, 2008, 12:05 AM
Even without the id this might work: