views:

1709

answers:

7

Hey, I have been trying for over a week now to slideToggle a table row when the 'more info' button is clicked but nothing seems to be working at all.

I'm new to Jquery so if anyone ca help me slideToggle the 'more-info-1', 'more-info-2', 'more-info-3' tr tags. the main problem is that those id's are created dynamically through php and I don't understand how to select them in Jquery - like using 'more-info-' or something.

I would like it to work like this example: Here minus the iframes of course.

The user will click the 'more info' button and then the 'more-info-' tr will slide down.

Here is the page source: (I don't know how to insert HTML properly on StackOverFlow, is there a special way of doing it - the code button does not work properly with HTML)

html

div id="output-listings"

div class="main-info"

    table class="listings"

        tbody

                tr id="more-info-1" class="mi-1"

                    td

                    div id="more-1" class="more-information"/div

                    /td

                /tr

                tr id="main-info-1"

                    tdLeftlane News/td

                    tdwww.leftlanenews.com//td

                    tda id="link-1" class="more-info-link" href="#"More info/a/td

                /tr

                tr id="more-info-2" class="mi-2"

                    td

                    div id="more-2" class="more-information"/div

                    /td

                /tr

                tr id="main-info-2"

                    tdMotor Authority/td

                    tdwww.motorauthority.com/ /td

                    tda id="link-2" class="more-info-link" href="#"More info/a/td

                /tr

                tr id="more-info-3" class="mi-3"

                    td

                    div id="more-3" class="more-information"/div

                    /td

                /tr

                tr id="main-info-3"

                    tdAutoblog/td

                    tdhttp://www.autoblog.com//td

                    tda id="link-3" class="more-info-link" href="#"More info/a/td

                /tr

        /tbody

    /table

/div

/div!--end output-listings--

/html

I would greatly appreciate the help, thank you so much.

A: 

From a quick glance it looks like you want something like this.

$('#link-1').click(function(){
    $('#more-info-1').slideToggle()
})

You can also generalize this script to work with all three by changing how the classes are set up, or by having the inner function parse the number of th link that is clicked and feed that into the inner jQuery call:

$('.more-info-link').click(function(){
    var id = $(this).attr('id');
    var num = id.substr(id.length-1,1);
    $('#more-info-'+num).slideToggle();
})
Craig
I have done something like this, but this will not work properly when I need to click on the second or third item. I would like jQuery to automatically get the id of the tr and only open that selected tr.
Ok I changed the answer so that it should work with all three. Does that work better?
Craig
A: 

Thanks, That works properly. The only thing is that the tr's are open when the page is loaded. Is there a way to keep them closed first and then open which ever one is clicked?

A: 

To keep the trs from being visible add style='display:none' to them.

Craig
A: 

Is there a way to do this dynamically, or is there a simpler way?

+1  A: 

Though craig's response works, you can limit your code and allow jquery to grab all of your TR elements within a certain scope, and parsing out the id as he suggested, etc.

$(".listings tbody tr").each(function(index) {
    // hide by default
    $(this).css({'display': 'none'});


    // set the onclicks
    $(this).click(function() {
      // your dosomething can change your appearance
      dosomething(the_id_you_parse_out);
    });
});

Not sure if thats working code, I just threw it together so you could get the gist of how to use jquery's selector.

brack
A: 

Depending on whether you need to assign a specific id to your elements for another purpose, you can actually do this without needing to give individual id's.

In this example, on load we first hide any tr that have the class toggleable. Then we tell jQuery to jump up the dom a couple of levels, and hide the next tr - this removes the need to call an id.

Example jQuery for this:

$(document).ready(function(){

$("#tableToggle tr.toggleable").hide();

$("#tableToggle .tableToggleButton").click(function(){
 $(this).parent().parent().next('tr').slideToggle();             
});

});

Example modified table:

<table id="tableToggle">
<tbody>
<tr>
<td><div class="tableToggleButton">More info 1</div></td>
</tr>
<tr class="toggleable">
<td>Main info 1</td>
</tr>
<tr>
<td><div class="tableToggleButton">More info 1</div></td>
</tr>
<tr class="toggleable">
<td>Main info 1</td>
</tr>
</tbody>
</table>
Alex Osborn
A: 

Since jquery code usually executes only when the DOM is ready, you'll always see the TR's even if only for a millisecond until your js code hides it if you don't use CSS initially to hide it.

So most of the contributors here probably answered your question. I'd just like to add that you could initially hide the TR's using CSS and then use JS to do the rest.

A good rule of thumb is to try and get your "default" pageview by only using CSS, and then add the rich functionality with the jquery code. Or at least that's what I'd do.

Ettiene