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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:58:47+00:00 2026-05-31T22:58:47+00:00

Suppose i have this structure of elements: <div class=parent> <div class=something1> <div class=something2> <div

  • 0

Suppose i have this structure of elements:

<div class="parent">
    <div class="something1">
        <div class="something2">
            <div class="something3">
                <div class="something4"></div>
            </div>
        </div>
    </div>   
</div>

And code like this:

$(".something4").click(function(){
   //i could do it like this...
   $(this).parent().parent().parent().parent().parent();
});

But that seems to be stupid, is there a better way to do this?

also i can’t just say $(.parent) because there are many divs like this with class parent in my page.

  • 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-05-31T22:58:48+00:00Added an answer on May 31, 2026 at 10:58 pm

    You could always use .parentNode (standard JavaScript). It’s generally a bad idea to use class names that coincide with function/variable names from the library you’re using (this goes for any language). Making your class names more unique is a better approach (for instance, “scparent” instead of “parent”, if the name of your application was “Super Calculator” or something). This avoids conflicts such as the one you’re describing.

    I would caution using .closest(), simply because you may create a function like this:

    function getParentElem() {
        return $(this).closest('div');
    }
    

    And it would grab the parent div‘s in your code just fine, but if down the road you add a table for displaying data, and you run the function through a child element of the table, you will have to create another implementation that selects the table element, because that’s what you now want:

    <div id="tableParent">
        <table id="dataTable">
            <tr id="target1">
                <td>Some data.</td>
            </tr>
        </table>
    </div>
    

    By using your function getParentElem() on the tr element, you’ll end up grabbing the div with id=”tableParent”, rather than the actual parent, which is the table element. So, unless you’ve delineated your parent classes appropriately all the way through your code (which can be a pain and isn’t always efficient), you may run into problems. Especially if at any point you’re creating elements programmatically, or reading in data from another 3rd-party library or script.

    Not saying it’s not good to use .closest()… just pointing out a possible “gotcha”.

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

Sidebar

Related Questions

Suppose I have this HTML structure: <div class=a> <div class=floated-left>...</div> <div class=floated-left>...</div> </div> I
Suppose I have this html markup: <div id=wrapper> <pre class=highlight> $(function(){ // hide all
Suppose you have a directory structure like this: A/ B/ a.1 b.2 c.3 I'm
Suppose I have a class structure like the following, where I have a temporary
suppose we have this class structure: public class BaseClass { public BaseClass() { Console.WriteLine(Base
Suppose I have this Windows wchar_t string: L\x4f60\x597d and L\x00e4\x00a0\x597d and would like to
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]
Suppose I have this (C++ or maybe C) code: vector<int> my_vector; for (int i
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
Suppose I have a bulleted list like this: * list item 1 * list

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.