Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:54:42+00:00 2026-06-12T23:54:42+00:00

This should be a nice easy one for someone – I have a table

  • 0

This should be a nice easy one for someone – I have a table row that slideToggles when an image is clicked. As the table is dynamic, there could be any number of rows so I have had to identify the areas to show/hide by classname.

Not a problem. However, I only ONE instance of the extrainfo div to be shown at once. As one is shown, any already visible should be hidden:

EDIT: Here is a fiddle: http://jsfiddle.net/shpsD/

Added HTML below.

    var toggleSpeed = 300;
    var expandImg = "../Images/expand.png";
    var collapseImg = "../Images/collapse.png";
    $(".moreless").click(function () {
        var detailsRow = $(this).parent().parent().next();
        detailsRow.find('.extrainfo').slideToggle(toggleSpeed);
        if ($(this).attr('src') == collapseImg) {
            $(this).attr('src',expandImg);
            $(this).closest('tr').removeClass('highlight_row');
        }
        else {
            $(this).attr('src',collapseImg);
            $(this).closest('tr').addClass('highlight_row');
        }
    });
});

–

<table>
    <tr>
        <th>Header</th>
        <th></th>
    </tr>
    <tr>
        <td>row 1</td>
        <td><img src="expand.png" class="moreless" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="extrainfo">
                EXTRA INFO!!
            </div>
        </td>
    </tr>    
    <tr>
        <td>row 2</td>
        <td><img src="expand.png" class="moreless" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <div class="extrainfo">
                EXTRA INFO!!
            </div>
        </td>
    </tr>
</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:54:44+00:00Added an answer on June 12, 2026 at 11:54 pm

    First hide the elements:

    $('table .extrainfo').slideUp();
    

    You could also use the :visible selector, if you want, though it doesn’t necessarily make things any faster, but perhaps a little more understandable:

    $('table .extrainfo:visible').slideUp();
    

    And then show:

    detailsRow.find('.extrainfo').slideDown();
    

    JS Fiddle demo.


    Edited with regards to the comment left by the OP (below):

    It works fine when switching between rows, but it is not possible to hide all rows, as if only a single row is expanded it slides up and then immediately down. Any ideas?

    I’m not sure, this was a pretty quick look once I’d finished work, but I might be over-doing things, but the following seems to work as you’d require:

    $(".moreless").click(function() {
        // caching variables:
        var that = $(this),
            table = that.closest('table'),
            row = that.closest('tr'),
            visInfo = table.find('.extrainfo:visible').length,
            extrainfo = row.next().find('.extrainfo');
    
        // I suspect this conditional is flawed, and redundant,
        // but essentially if there's already a visible element *and*
        // the next '.extrainfo' element in the next row is visible,
        // then we're having to toggle/close
        if (visInfo == 1 && extrainfo.is(':visible')) {
            // we're working on the row that's already visible:
            extrainfo.slideToggle(toggleSpeed);
            row.toggleClass('highlight_row');
        }
        else {
            // not toggling the same table-row, so tidying up previously
            // visible elements (if any)/removing 'highlight_row' class
            // and also setting the src of the image to the expandImg
            var highlighted = table.find('.highlight_row');
            highlighted.find('.moreless').attr('src',expandImg);
            highlighted.removeClass('highlight_row');
            table.find('.extrainfo').slideUp(toggleSpeed);
    
            // now we're showing stuff/adding the class
            extrainfo.slideDown(toggleSpeed);
            row.addClass('highlight_row');
        }
        // this effectively toggles the src of the clicked image:
        that.attr('src', function(i,v) {
            return v == expandImg ? collapseImg : expandImg;
        });
    });​
    

    JS Fiddle demo.

    (I’ll add references and answer further questions once I’ve eaten…sorry!)

    References:

    • attr().
    • closest().
    • find().
    • slideDown().
    • slideUp().
    • :visible selector.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is an easy one for someone out there. I have a
This should be a simple one: I have an observableArray object called To in
edit: On odd change that someone is reading this, I'd like to add one
This should be so simple... I thought it would be nice to try the
This should be a no brainer but I have a small mystery with the
This should be simple, but I'm getting confused. I have a parent/child tables -
This should be a really, really simple one, I would assume. I'm throwing together
This should be a pretty straightforward question. I have the following code, which forms
This should be quite simple, but I can't work out the syntax. I have
I don't have nice short code examples to display with this post. I'm running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.