-1

just see the code below

i just saw an video on youtube about this by its not working in my case

<head>
  <script>
    var r = document.getElementById("para").document.getElementsByTagName("span");
    console.log(r);
  </script>
</head>

<body>
  <p id="nn">
    <span>demand</span>
    <span> demand 2</span>
    <b> block of bold</b>
  </p>
</body>

it should return the array containing all span tags in id="para".

adiga
  • 28,937
  • 7
  • 45
  • 66
  • 1
    Also, you need to fix `document.getElementById("nn").document.getElementsByTagName("span");` to be `document.getElementById("nn").getElementsByTagName...` – Nick Parsons Oct 24 '19 at 11:57
  • Change `para` to `nn` and remove the `.document` after `getElementById("para")` – adiga Oct 24 '19 at 12:06

1 Answers1

1

You dont have any element with the ID of "para" what you mean is probably the element with the id "nn". Also if u try to find some elements within another element then dont call that method on "document" but rather on the previously selected element like so:

var r= document.getElementById("nn").getElementsByTagName("span");
console.log(r);
<html>

<head>

</head>
<body>  
  <p id="nn">
      <span>demand</span>
      <span> demand 2</span>
      <b> block of bold</b>
  </p>
</body>

</html>
Mischa
  • 1,307
  • 7
  • 12