search

Home  >  Q&A  >  body text

How to hide specific div elements based on conditions? (without using styles)

So I have 2 divs

<div id="@components">
Test
</div>

<div id="@documents">
if(documents.count > 0){
then hide div id components 
}
</div>

So essentially what I want to do is hide the "component" div within the code of the "document" div. I know I can set display:none in the style but how do I hide the component's div directly?

P粉006540600P粉006540600441 days ago535

reply all(1)I'll reply

  • P粉190883225

    P粉1908832252023-09-16 12:13:32

    Use window.document.getElementById("@components") Get your @component div anywhere on the page, then you can use it to do what you want required operation.

    <div id="@components">
    Test
    </div>
    
    <div id="@documents">
    if(documents.count > 0){
        //then hide div id components
        let componentsDiv = window.document.getElementById("@components");
        if(componentsDiv)
            componentsDiv.style.display = "none";
    }
    </div>

    reply
    0
  • Cancelreply