-2

I am trying to select an element with 'Selector name' and then delete it.

But I am unable to select the element :

Chrome give me Selector :

body > zea-hub > div > div > zea-my-accounts > zea-section > section > div > div > div:nth-child(1)

To select it I tried to use :

document.querySelector("body > zea-hub > div > div > zea-my-accounts > zea-section > section > div > div > div:nth-child(1)")

Which return null

and

document.querySelectorAll("body > zea-hub > div > div > zea-my-accounts > zea-section > section > div > div > div:nth-child(1)")

Which return NodeList[] I bet this is a good return

So I tried this way to delete this element :

var ChromeSelector = document.querySelectorAll("body > zea-hub > div > div > zea-my-accounts > zea-section > section > div > div > div:nth-child(1)");

ChromeSelector.parentNode.removeChild(ChromeSelector)

but it does not work... Am I missing something ?

var ChromeSelector = document.querySelectorAll("body > section > div > div > div:nth-child(1)");
alert(ChromeSelector)
ChromeSelector.parentNode.removeChild(ChromeSelector)
<section MySectionTest>
   <div _Firstdiv class="container">
     <div _Seconddiv class="Column">
       <div FirstSectionElement class="section">
         <h1 _ngcontent-kwd-c95="">Test</h1>
       </div>

       <div FirstSectionElement class="section">
         <h1 _ngcontent-kwd-c95="">Test2</h1>
       </div>
      </div>
   </div>
</section>
TourEiffel
  • 4,034
  • 2
  • 16
  • 45
  • 2
    You need to read [How to ask a good question](https://stackoverflow.com/help/how-to-ask). There's no way anybody can reproduce the problem from the scant information you've provided in the question. – Quentin May 09 '20 at 12:47
  • Click edit, then `[<>]` snippet editor and post a [mcve] – mplungjan May 09 '20 at 12:48
  • 1
    You cannot remove a nodelist. You can remove each node - if you get a nodelist, check the length - if it is 0 then your selector is not correct – mplungjan May 09 '20 at 12:49
  • We have no idea until you post the relevant HTML – mplungjan May 09 '20 at 12:53
  • 1
    It is highly unlikely that querySelector returns null and querySelectorAll returns a nodelist with length > 0 - could this be a timing issue? Does it exist when you look for it? – mplungjan May 09 '20 at 12:55
  • 1
    Duplicate of https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return – Quentin May 09 '20 at 13:00
  • @Dorian — Your code doesn't even try to do that – Quentin May 09 '20 at 13:02
  • 1
    @Dorian — The duplicate explains. As did mplungjan in a comment 15 minutes ago. – Quentin May 09 '20 at 13:05
  • Try reading the accepted answer on the duplicate. – Quentin May 09 '20 at 13:14
  • Useful to tell us what exact element you wanted to delete. For example by adding "Delete me" inside the div – mplungjan May 09 '20 at 13:18

1 Answers1

-1

The answer was thank you @Quentin :

var ChromeSelector = document.querySelectorAll("body > section > div > div > div:nth-child(1)");

ChromeSelector[0].remove();
TourEiffel
  • 4,034
  • 2
  • 16
  • 45