How to delete an a tag inside an element with Javascript, without giving it an identifier?

I have an a tag which is nested inside a div that has the class “divi” and I want to remove only the a tag from it, keeping all other content inside it.

Yet, this a tag doesn’t have an identifier (id, class) and I prefer not to give one (a long theming story why).

This is what I’ve done so far, but it doesn’t work:

let removeGpLogo = document.querySelector(".divi");
removeGpLogo.a.style.display = "none";

I can see why my code is illogical — A has no identifier so identifier-dependent codes like “display: none” won’t work on it, but what will?..

My question:

How could I hide the tag without giving it an identifier, if at all?

You can use .querySelector() on any element like you can on the document:

removeGpLogo.querySelector('a').style.display = 'none'

Or if you don’t need removeGpLogo otherwise, you can actually get the a element with a usual CSS selector as well:

document.querySelector('.divi a').style.display = 'none'
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.