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 8359303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:06:16+00:00 2026-06-09T11:06:16+00:00

I am trying to show/hide the next two rows of a table using jquery.

  • 0

I am trying to show/hide the next two rows of a table using jquery. The code below currently shows the tr with class – quest_image, but not class quest_text.

<table id="my_table">
<tr>
    <td><img alt="Show/Hide" src="img.gif" class="ShowHide"></td>
    <td>text 1</td>
</tr>
<tr class="quest_image">
    <td colspan="2">hidden text 1</td>
</tr>
<tr class="quest_text">
    <td colspan="2">hidden text 3</td>
</tr>
<tr>
    <td><img alt="Show/Hide" src="img.gif" class="ShowHide"></td>
    <td>text 2</td>
</tr>
<tr class="quest_image">
    <td colspan="2">hidden text 4</td>
</tr>
<tr class="quest_text">
    <td colspan="2">hidden text 2</td>
</tr>

</table>
<script type="text/javascript" src="../jquery/jquery/jquery-1.7.2.js"></script>
<script type="text/javascript">
// bind click event ONCE to the table
// not to every .ShowHide
$('#my_table').click(function(event){

    // find where the click originated from
    var trigger = event.target;

    // if it has a class of "ShowHide"
    // THEN toggle the next row
    if(trigger.className == 'ShowHide')
    {
        // toggle away
        $(trigger).closest('tr').next('.quest_text').toggle();
        $(trigger).closest('tr').next('.quest_image').toggle();
    }
});
</script>

Demo: http://jsfiddle.net/7TzJL/

Why does one toggle work, but not the other?

  • 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-09T11:06:18+00:00Added an answer on June 9, 2026 at 11:06 am

    next() returns the immediate next element if the selector matches. It doesn’t select the closest element with matching selector. You need to use nextAll() and limit it to the first match.

    Also, jQuery 1.7 has .on() so you can drop the convoluted class check and bind the event to the classes directly. Don’t forget to wrap everything in a document.ready event.

    $( function() {
        $('#my_table').on( 'click', '.ShowHide', function() {
            var $trigger = $( this );
            $trigger.closest( 'tr' ).nextAll('.quest_text:first').toggle();
            $trigger.closest( 'tr' ).nextAll('.quest_image:first').toggle();
        });
    });
    

    Demo: http://jsfiddle.net/MaPaX/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to show/hide table rows using jquery. First two rows work. The third row
I have a question about usability/design. I currently am using some JQuery to hide/show
Trying to show/hide severals divs using jquery after each news on my page. <div
I'm trying to show/hide items within a collection using a jQueryUI slider. The slider
I am trying to hide/show a class of elements in a form depending on
I'm trying to create a standardized show/hide element system, like so: <div class=opener popup_1>Click
I'm trying to adapt Andy Langton's show/hide/mini-accordion ( http://andylangton.co.uk/jquery-show-hide ) to work within a
I am trying to show/hide the child class when parent is hovered. I have
I just started programming and learning Jquery, I'm trying to build a simple show/hide
I'm trying to use jQuery's slideToggle function to show or hide a panel which

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.