views:

1778

answers:

2

Hi all,

I'm trying to adapt Andy Langton's show/hide/mini-accordion (http://andylangton.co.uk/jquery-show-hide) to work within a table. I'm wanting to create a list of events with a confirmation form attached to each event. Upon clicking on the 'confirm' button in the last cell or the row, I would like the form associated with this particular event to be revealed.

Andy's code uses

$('.toggle')
    .prev()
    .append('<a href="#" class="toggleLink">'+showText+'</a>');

to dynamically add the toggle link (the confirm button) just before the hidden form. This, however, adds the link within the table row and not in a cell. I have therefore changed it to

$('.toggle')
    .prev()
    .append('<td><a href="#" class="toggleLink">'+showText+'</a></td>');

The link is now in the correct place but does now not invoke the show/hide of the form. When it was placed incorrectly the functionality worked, despite not being quite right. I feel that the selector calling the toggle action is not correct but I don't know how to correct it. It is currently

$(this)
    .parent()
    .next('.toggle')
    .toggle('slow');

This is essentially how the source looks...

<table id="training-events">
<tr>
   <th>Date / Time</th>
   <th>Event / Venue</th>
   <th>Cost</th>
   <th>Confirm</th>
</tr>
<tr class="event" valign="top">
    <td class="date">Mon, 10 August 2009<br>03:30 PM - 05:30 PM</td>
    <td><h5>Regional Director Meeting</td>
    <td>No Charge</td>
    <td><a href="#" class="toggleLink">Cancel</a></td>
</tr>
<tr style="display: none;" class="toggle">
   <td colspan="4">
      ** FORM **
   </td>
</tr>
</table>
A: 

This works for me if I remove the style="display:none;" from the toggle TR.

You'll notice in the example he's also doing:

 $('.toggle').hide();

This is what is hiding the toggle classed elements.

chsh
Which "This" are you referring worked for you?
Crescent Fresh
Whoops, that was a failed paste. I was attempting to add similar code to what Doomspork had provided with the two parent() calls.
chsh
+1  A: 

You need something like:

$(this).parent().parent().next('.toggle').toggle('slow')

or

$(this).closest('tr').next('.toggle').toggle('slow');

You're only making a single parent() call which brings you to the TD, you need to step up to the TR.

Doomspork
Thanks, that worked! :-) It's still, however, not displaying correctly in that despite the show/hide row being set to span 4 columns, it doesn't...but that's prob something i've screwed up- http://development.dekken.co.uk/bni/training/Do you happen to know how to get this toggling without animating in? I'd like it to appear as one rather than bit by bit.thanks again.
Dekken
To show/hide without the animation you can just use toggle(). As for the rows there are two things I can think of: First, since you always intend to have a form, you might as well have it generated server side rather than relying on the javascript to do it. Second, if for some reason you cannot add the form in the html or generate it server side, it might be better to add the DOM elements individual rather than using append for a string of HTML.
Doomspork