Like taking a father’s son?

Asked

Viewed 1,172 times

-2

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

Here’s how it works: When a date has an unavailable time the system warns.

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 to Browse and Find 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>

  • 4

    How do you know what to look for? You will look for the child element by id? and where do you know if you should look for 08:00 or by 09:00?

  • That part’s no problem.

  • With Js I get the elements. From date and time. Just need to compare the date (id) to the time (id daughter)

  • 2

    I didn’t get your answer. What are you looking for in the child elements? If you want to know all you can use elemento.children. If you’re looking for one, specific, what do you look for in it? for in that case there may be a more correct answer.

  • Will always be a specific.

  • 1

    @Very badly done Uriel this relation of Id. Can improve by putting something statical, example "data_12-05-2016" , "hora_08:00" .

  • @Uriel takes another look at my questions here in the comments. You understand what I’m trying to clarify and it’s not clear to me in your question?

  • Sergio, the question is very simple. I want to check if a date has an hour inside. The date is an ID the time that is inside tbm is. Via JS I take the date field and its value. Then pick up from the time field its value. .

  • @Uriel then you have in a variable the time you want and you need to find the child element that has that time as id that’s it?

  • The problem has been solved

  • @Uriel ok, great. I still think there are doubts and that you could better understand your problem. If you want to learn and understand the problem better give feedback to questions from experienced people who want to help you.

Show 6 more comments

2 answers

1


Assuming you have how to get the ID, follow a proposed solution. Note that in the example, I changed the attribute display:None for 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>
  • Thank you. I made a change and resolved.

0

Next uriel, if you are going to repeat the hours for example, 08:00 be on two different dates I advise 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 it is expected). And if it does not repeat just use the own getElementById.

Browser other questions tagged

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