views:

125

answers:

2

I use php to display a table of data drawn from my mysql database. I have a while loop that loops through each row and spits out the user data.

I want to have a more details button that when pressed will drop down a div box displaying "more details" about the user. The problem I've run into is when I create the div to hold the more details I can't figure out how to make it unique for each user.

When I click the more details link it always opens the top most div only... because my id's aren't unique. To make the div's unique I could just echo a count to them and the href but I'm not sure how to pass that into jquery as parameter so that when I click the more details link next to a users name it will display the more details div specific to that user.

I've been toying with the below code without much luck, help appreciated. Thanks.

   <script type = "text/javascript">
    $(document).ready(function(){
        $('a.moreDetails').click(function() {
         $("#moreDetails").toggle('fast');
        });
    });
    </script>


while()
{

user info...  more details href
<div></div> to display below once more details href has been clicked.

}
+1  A: 

How about:

$('a.moreDetails').click(function() {
    $(this).next().toggle('fast');
});

This will take the next element after the anchor link (<a>) and toggle it.

Then the HTML might look like

User 1 (username) <a href="#" class="moreDetails">(details)</a>
<div class="details">More details about User 1</div>
jgeewax
Thanks, I had a bit of trouble at first because I had the href inside a table and when I clicked it the div would not appear. But once i took it out of the table and put it directly above the div it worked. Is there a way to specify the next() to f ind just div elements and not table cells, I'm presuming that's what it was doing.
payling
You could hop up to the parent div, then get the next TD element and toggle that:`$(this).parent().next().toggle('fast');`jQuery gives you all sorts of tools to navigate from one element to another. Take a look at http://docs.jquery.com/Traversing for more info.
jgeewax
A: 
gabtub