0
HTML:   

    <script type="text/javascript">
            invis();
          </script>
        
          <div id="cok" class="cookiesTXT">
            <h2>Diese Seite nutz Cookies, unsere Cookie Richtlinien finden sie <a href="/cookies">hier</a>
            <br> Sind sie damit einverstanden? </h2>
            <button type="button" name="button" onclick="accept()">Akzeptieren</button>
            <button type="button" name="button" onclick="invis()">Ablehnen</button>
          </div>
    
    
    
    JavaScript:
    
    function invis(){
      document.getElementById("cok").style.display = "none";
    }

If I open the Site the function invis is not working and the div isn't invisible, but if I click the last Button labeled "Ablehnen" then the function invis work well

Why is this, and how can I make this work ?

Paul .M
  • 3
  • 3

2 Answers2

0

Try this

<div id="cok" class="cookiesTXT">
    <h2>Diese Seite nutz Cookies, unsere Cookie Richtlinien finden sie 
<a href="/cookies">hier</a>
        <br> Sind sie damit einverstanden? </h2>
    <button type="button" name="button" onclick="accept()">Akzeptieren</button>
    <button type="button" name="button" onclick="invis()">Ablehnen</button>
</div>

<script type="text/javascript">
    let isShow = false;
    function invis() {
        isShow ? document.getElementById("cok").style.display = "display" : document.getElementById("cok").style.display = "none";
        isShow = !isShow;
    }
    invis();
</script>
0

Always use script tag after HTML Element. Just like this

<div id="cok" class="cookiesTXT">
    <h2>Diese Seite nutz Cookies, unsere Cookie Richtlinien finden sie <a href="/cookies">hier</a>
        <br> Sind sie damit einverstanden? </h2>
    <button type="button" name="button" onclick="accept()">Akzeptieren</button>
    <button type="button" name="button" onclick="invis()">Ablehnen</button>
</div>

<script type="text/javascript">
    invis();
    function invis() {
        document.getElementById("cok").style.display = "none";
    }
</script>