0

I'm trying to remove a tag "a" within a specific div.

How can I access to the tag?

<script>
    window.onload = function myFunction() { 
        var elem = document.getElementById("divname");
      var a_tag_elem = elem.getElementsByTagName("a");
      a_tag_elem.remove();    
} 
</script>
  • The issue is probably that getElementsByTagName('a') returns a list so you need to do [0] afterwards – RobPethi Feb 11 '20 at 12:02
  • Try this: `var div = document.getElementById("divname"); var a = div.querySelector("a"); div.removeChild(a);` – uminder Feb 11 '20 at 12:35

1 Answers1

-1

Try this:

a_tag_elem[0].remove();
Ivar
  • 6,138
  • 12
  • 49
  • 61
Cem Tuğut
  • 97
  • 6