V
V
Vladimir Yurchenkov2021-07-02 13:57:04
css
Vladimir Yurchenkov, 2021-07-02 13:57:04

How to hide DIV without id or class in JS?

Hi

Can you please tell me how to hide a div that only has style.
This block hangs at the beginning of the body which has an id. The block itself contains a ton of styles. It would be possible to hide it as the first child block, but this block is not always there. This is a banner from the CMS so climbs. There is no access to the code, but there is access to html through a macro, that is, I can edit styles for c id or class blocks. You need to find it by style="..." and hide (in extreme cases, change the text) Perhaps you can search by the text in the block or by styles, but I don’t know how. Found this code..

<div style="margin:5px;padding:5px">Text</div>


var els = document.getElementsByTagName('div');
for (var i = 0; l = els.length; i < l; i++) {
  if (els[i].innerHTML == 'style....') {
    els[i].parentNode.removeChild(els[i]);
  }
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel Shvedov, 2021-07-02
@EPIDEMIASH

If it is as stupid as possible, then here is https://stackblitz.com/edit/js-mfhn7x?file=index.js

C
Chipekwe, 2021-07-02
@XanXanXan

[...document.querySelectorAll('div')].find(e => e.textContent === 'Text').style.display = 'none';

V
v3shin, 2021-07-02
@v3shin

body > div:first-child {
    display: none;
}

Also, don't use a useful div as the first element.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question