View Full Version : Expand/Collapse using jQuery

11-06-2008, 05: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


11-07-2008, 12:03 PM
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.

11-07-2008, 09: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.

11-08-2008, 12:05 AM
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

11-08-2008, 01:05 AM
Even without the id this might work: