Pick up "child" element from a DIV

Asked

Viewed 23,399 times

0

I have seen several examples of how to do this here in the stack, but I don’t know why the answers don’t clarify much, then I’d like to to ask again how could I use JS to get the "tag ID <A> of the following DIV, REMEMBERING that my code is in the main DIV.

<li id="sem-importancia">
 <DIV id="principal">
   <a id="um-teste"> </a>
 </DIV>
</li>

when I use the following js, it works perfect and caught a tag above...

var item_id = elem.parent('li').attr('id');

SOON what code to use to catch the tag below ? a

< a id="um-teste" > < /a >

thank you!

  • 2

    Are you using jQuery? This does not work in Javascript: elem.parent('li').attr('id'); Solution with jQuery is one thing, with pure Javascript is another. I saw an answer that you accepted earlier, about getting an element ID, that the answer is wrong if it’s pure JS (If it worked for you, it’s because you used jQuery. When it’s like this, you have to put in the tags that is jQuery). I would suggest to correct that and this one, if you want in jQuery too. Although for these simple things, the ideal is pure JS.

  • 1

    If you are in jQuery you have this related question: en.stackoverflow.com/questions/116446/how-to-select-the-sibling elements

  • hello Bacchus and Gagriel, good .. as I am new please forgive me these mistakes... I swore that I was soh JS... I have noticed now. that this project I’m working on.. has parts of jquery and parts in JS... I’m trying to learn... I also think the idella is pure JS.. or at least until I learn...

1 answer

9


The hierarchy of objects, and structures html, xml, etc, follow a logic very similar to that of a person (or any object). The component has a "parent", or if, some component that gave rise to it or the component to which it belongs, each element has only 1 parent.

When the elements are within a specific element, they are called child elements, in which case a parent can have several children, which is the logic to place several elements within a.

In the javascript the elements of the DOM table have the attribute parentElement responsible for collecting the parent element. It also has the attributes of the child elements that would be:

  • childNodes - returns a Nodelist of the child elements.
  • childElementCount - returns the number of children that that element has
  • Children - returns a vector with the child elements
  • I didn’t quite understand if you wanted an explanation, or the code to get the elements, but this should solve.

    var el = document.getElementById('elem');
    
    alert(el.parentElement.id);
    alert(el.children[0].id);
    <ul id="pai">
      <li id="elem">
        <div id="filho">Item</div>
      </li>
    </ul>

    • Hello Brumazzi I was looking "first" for the code, so I could fill...... but your answer was a true light, for people who are starting it .. and can’t even ask the right question... I don’t know how to thank you! I wish I had more people like Voce (and some other friends I met here) explaining in a simple, OBJECTIVE way and with the example of the code... I repeat.. " agent we are beginners..." rsrsrs BRIGADEO!

    • 1

      I still get a lot of help and tips in the OS, as it’s been a little while since I move with programming, I still know little, and constantly learn a lot with questions and answers. " Programming is a vast and endless area, "to be average, we must strive day after day. @Robervalsena 山 本

    • Man, really good! Just to help anyone who needs it, in my case I used it: document.getElementById('menu-topo').children[0].children[0].innerHTML = '<i class="fa fa-lg fa-home"></i>';. Note that children[0] appears twice.

    Browser other questions tagged

    You are not signed in. Login or sign up in order to post.