How to get a child from a parent div?

-2

Next, I have a series of Lis created dynamically. What I need to do with pure JS is simply to find the child of the parent div.

It works like this: When a date is unavailable, the system will notify you.

The date is i parent ID, and the children are IDs in hours.

With PURE Js (I'm learning why I want pure and not Jquery).

How do I go about traversing and finding the child ID?

<li style="display:none;" id="12-05-2016">Para o dia: 12-05-2016
    
    <ul><li  id="08:00"> Esta hora esta indisponivel: 08:00</li></ul>

    <ul><li  id="09:00"> Esta hora esta indisponivel: 09:00</li></ul>

</li>

<li style="display:none;" id="13-05-2016">Para o dia: 13-05-2016

</li>
    
asked by anonymous 11.05.2016 / 21:26

2 answers

1

Assuming you can get the ID, a proposed solution follows. It is worth mentioning that in the example, I changed the display: none attribute to display: block

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>

<body>
  <ul>
    <li style="display:block;" id="12-05-2016">Para o dia: 12-05-2016
      <ul><li  id="08:00"> Esta hora esta indisponivel: 08:00</li></ul>

      <ul><li  id="09:00"> Esta hora esta indisponivel: 09:00</li></ul>
   </li>

    <li style="display:block;" id="13-05-2016">Para o dia: 13-05-2016>

    </li>
  </ul>
  <script type="text/javascript">
    /* Obtém o elemento PAI */
    var elem = document.getElementById("12-05-2016");

     /* Obtém os elementos FILHOS (childNode) filtrando pela ul */
    var subElem = elem.getElementsByTagName("ul");

    /* Apresentar cada item dentro do ChildNode (FILHO)*/
    for(i = 0; i < subElem.length; i++)
      console.log(subElem[i].getElementsByTagName("li")[0]);
  </script>
</body>
</html>
    
11.05.2016 / 22:07
0

Next Uriel, if you are repeating the hours for example, 08:00 am on two different dates I advise you to spend those hours you want as a class, then use:

var pai = document.getElementById('12-05-2016');
var filhos = pai.getElementsByClassName('08:00');

Because using the id means that it will be unique (at least that's what you expect). And if you do not repeat, just use getElementById itself.

    
11.05.2016 / 21:36