tags:

views:

226

answers:

2

I have a ticket management program that I am trying to add 'accordian' style functionality to using jQuery. This program is written in PHP. The output HTML looks like this:

<div class="eachticketwrapper">
     <div class="simpleticketview">
           <ul class="displayinline ticketsummarybar">
                    <li class="col0"><a href="#" class="toggleLink">E</a></li>
                       //...
           </ul>
      </div> <!-- simpleticketview -->
<br class="clear" />
<div class="toggle"> <!-- hiddenticketview -->
     //...
</div>

This repeats dozens of time on a page.

My jQuery code looks like this:

$(document).ready(function() {
   $('div.toggle').hide();
   $('.toggleLink').click(function() {
        $(this).next('div.toggle').slideToggle('fast');
   return false;
   });
});

When I click on the E it will not slide the next div. in fact nothing happens.

I have tried to use : $('div.toggle').slideToggle('slow');

And this causes all divs to toggle. But i need it to target the NEXT div.

Thanks in advance.

+1  A: 

you need to give your 'toggle' div a unique id and match it w/your link id. then target those ids:

<div class="eachticketwrapper">     
    <div class="simpleticketview">           
        <ul class="displayinline ticketsummarybar">
                <li class="col0"><a href="#" id="link-1234" class="toggleLink">E</a></li>
                   //...           </ul>
  </div> <!-- simpleticketview -->
  <br class="clear" />
  <div id="ticket-1234" class="toggle"> <!-- hiddenticketview -->     
   //...
  </div>

jQuery:

$(document).ready(function() { 
    $('div.toggle').hide(); 
    $('.toggleLink').click(function() { 
        var id = $(this).attr("id").split("-");
        $("#ticket-" + id[1]).slideToggle('fast'); return false; 
    }); 
});
Jason
Thank you for the response Jason!Is there any way that I can avoid providing a unique id? I thought that the beauty of jQuery's power came in the fact that I can simply say the next .toggle div? True of False?
DodgerUSMC
you _can_ do .toggle div, but there is no way for it to know which div you're talking about. it would be different if your link was in your div, then you could make a call to the .parents() like kgiannakakis is suggesting
Jason
btw, if you look at the SO sourcecode, you'll see they use the ids the way i've suggested too
Jason
Jason! You are the man! One change: the addition of the prefix "ticket-" to the id selector in the last line. $("#ticket-" + id[1]).slideToggle('fast');Thanks again for all of your help!
DodgerUSMC
ah, yeah, true i forgot that part. but glad i could help :)
Jason
A: 

You can get the div without an id, but it will be somehow complicated:

$(this).parents().find("div.eachticketwrapper").nextAll().find("div.toggle:first")

This depends on the structure of your document. You may need to adapt it, if you have more than one eachticketwrapper elements. It is better to use an id.

kgiannakakis