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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:25:30+00:00 2026-06-12T16:25:30+00:00

Please take a look at the snippet below: <div> <div></div> <div><!– my target node

  • 0

Please take a look at the snippet below:

<div>
    <div></div>
    <div><!-- my target node -->
        <div><!-- not my target node -->
            <img /><!-- my source node -->
        </div>
    </div>
</div>

As you can see the img-elment has two enclosing divs. I want the first of those two enclosing divs to be considered the “real” parent (the one I need to find) of the img-elment because it has a brother div before so the search ends and the brother div and the outer enclosing div are ignored.

In the case there are no siblings at all, the outer div has to be yielded; in the case the element is not enclosed, the element itself has to be yielded.

I just would like to know how to target the element as I explained via JavaScript.

  • 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-12T16:25:31+00:00Added an answer on June 12, 2026 at 4:25 pm

    So it sounds like you want the first ancestor that has siblings elements. If so, you can do it like this:

    var parent = img.parentNode;
    
    while (parent && !parent.previousElementSibling && !parent.nextElementSibling) {
        parent = parent.parentNode;
    }
    

    Or perhaps more appropriately written as a do-while loop:

    do {
        var parent = img.parentNode;
    } while (parent && !parent.previousElementSibling && !parent.nextElementSibling);
    

    So the loop will end when it finds one with at least one sibling element, or when it runs out of ancestors.

    If you know if the sibling comes before or after the parent, you can just test for one or the other.


    Also note that you’ll need a shim for the ***ElementSibling properties if you’re supporting legacy browsers.

    You can make a function that will do this:

    function prevElement(el) {
        while ((el = el.previousSibling) && el.nodeType !== 1) {
            // nothing needed here
        }
    
        return el;
    }
    
    function nextElement(el) {
        while ((el = el.nextSibling) && el.nodeType !== 1) {
            // nothing needed here
        }
    
        return el;
    }
    

    Then use the functions like this:

    do {
        var parent = img.parentNode;
    } while (parent && !prevElement(parent) && !nextElement(parent));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at the screenshot below and see if you can tell
Please take a look at image below All I want is somehow organize Div
Please take a look at the code snippet below. I want the 2nd MongoDB
Please take a look at this fiddle: http://jsfiddle.net/hughbe/QYGcq/ As you can see, the label
Please take a look at the code snippet below: interface IFoo<E>{ void doFoo(E env);
Please take a look at my fiddle : The .pos_absolute div is under the
can someone take a look at this please http://jsfiddle.net/bloodygeese/ecscY/56/ it looks like I have
Please take a look at this topic : Can I calculate the average of
Please take a look at this link: http://bethhaim.spin-demo.com/ I can't figure out why there
please take a look at the following snippet: public IEnumerable<T> Query(Expression<Func<T, bool>> filter) {

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.