Get "child" element from a DIV

0

I have seen several examples of how to do this here in the stack, but I do not know why the answers do not clarify much, then I would ask again how I could use JS to get the tag <A> of the following DIV, REMINDING 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 I get a tag above ...

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

LOGO what code to use to get the tag below? to

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

Thank you!

    
asked by anonymous 05.04.2016 / 09:02

1 answer

6

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 some component that gave birth to it or the component to which it belongs, each element has only one parent.

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

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

  • childNodes - returns a child elements NodeList.
  • childElementCount - returns the number of children that element has
  • children - returns a vector with child elements
  • I did not understand it right 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>
        
    05.04.2016 / 09:44