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

  • SEARCH
  • Home
  • 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 7738309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:13:52+00:00 2026-06-01T08:13:52+00:00

I have the following HTML and I need to get the first div parent

  • 0

I have the following HTML and I need to get the first div parent of the object (collapse)

If this parent has a class of (ZeroClass) .. I want to have the next div parent ..

<div>
  <div id = "DeadCode">
    <div class = "ZeroClass">
      <table class = "zeroTable">
         <...tableData...>
      </table>
    </div>
  </div>
</div>

the jQuery Function :

function ResizeGrid(){
    var collapse = $(".zeroTable").parent();

    if ($(collapse).hasClass("ZeroClass")) {
        collapse = $(collapse).parents($("div"));
    }
}

Everything goes fine but when passing through the if statement, it makes Collapse Null .. any ideas?

  • 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-01T08:13:53+00:00Added an answer on June 1, 2026 at 8:13 am

    Use parent for the first step and closest for the second:

    function ResizeGrid() {
        var $collapse = $(".zeroTable").parent();
    
        if ($collapse.hasClass("ZeroClass")) {
            $collapse = $collapse.closest("div");
        }
    }
    

    Your problem lies in this row:

    collapse = $(collapse).parents($("div"));
    

    You first fetch ALL div elements $("div") and then tell $(collapse).parents() to match one of them.

    My code:

    $collapse = $collapse.closest("div");
    

    Tells jQuery to find the closest parent div.

    It’s a good practice to prefix jQuery variables with $ to help keep track of which vars are jQuery objects and which are regular DOM nodes.

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

Sidebar

Related Questions

I have the following HTML markup: <body> <div class=wrapper> <div class=head> <p class=title>title</p> <a
I have the following html code on the page received by mechanize (agent.get): <div
I have a sample HTML like the following, <div class=class1></div> <div class=class2 class3></div> And
I have the following HTML where I need the LI elements to vertically display
I have following HTML <tr id=r1 class=l1> <td><img class=plusminus id=r1c1 src=assets/images/plus.png border=0 />3/10/12</td> <td></td>
i have the following html , and i want to hide all lsHeader which
I have the following HTML: <span id=UnitCost5>$3,079.95 to $3,479.95</span> And i want to use
I have the following HTML: <div id=mydiv> </div> I would like to load content
Let me brief you the whole requirement first. I have the following HTML code:
I have the following DOM structure: /*:DOC += <div id='testDiv' class='testDivClass'><div id='innerTestDiv'></div></div><span id='testSpan' class='testSpanClass'><span

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.