visibility:Visible in div javascript

Asked

Viewed 1,265 times

0

I have the following div that this hidden:

  <div class="card" id="cnpj_erro" style="visibility:hidden">
       <div class="body">
            <div class="header bg-red">
                <h2>
                    CNPJ inválido <small>Confira os números</small>
                </h2>
            </div>
       </div>
  </div>

And the function:

<script>
        function verificarValidacaoCNPJ() {
            debugger;
            var valCNPJ = !document.getElementById("txCNPJ_cad_emp") ? "" : document.getElementById("txCNPJ_cad_emp").value;
            if (!validarCNPJ(valCNPJ)) {
                document.getElementById('cnpj_erro').setAttribute('style', 'visibility:visible');
            }
            else {
                document.getElementById('cnpj_erro').setAttribute('style', 'visibility:hidden');
            }
        }
    </script>

It is validating right, no problem, but I am not able to change the attribute 'style', 'visibility' so that it is displayed or hidden. Debugging when it enters the option document.getElementById('cnpj_erro').setAttribute('style', 'visibility:visible');

returns Return value: undefined

  • Have you tried using document.getElementById('cnpj_erro').style.visibility = "visible"?

  • was the first one I tried, also tried document.getElementById('cnpj_erro').visibility = "visible"

1 answer

0


As stated in the comments, for you to change an element style property, just do:

document.getElementById('cnpj_erro').style.visibility = "visible";

Take the example:

var visible = false;

function toggle() {
  if (!visible) {
    document.getElementById('cnpj_erro').style.visibility = 'visible';
    visible = true;
  } else {
    document.getElementById('cnpj_erro').style.visibility = 'hidden';
    visible = false;
  }
}
<div class="card" id="cnpj_erro" style="visibility:hidden">
  <div class="body">
    <div class="header bg-red">
      <h2>
        CNPJ inválido <small>Confira os números</small>
      </h2>
    </div>
  </div>
</div>

<button onclick="toggle()">Click</button>

If you tried to do it and it didn’t work, you must have done it wrong.

  • I did exactly the way you put it, I copied your code until, but it still doesn’t work, it enters the function without problem goes through Visible = true without problems, I know I must be doing something wrong, I will redo everything from the beginning to try to see what is.

  • So mount a [mcve] of how you’re doing and ask the question. It should work, as demonstrated.

  • was wobble my, had a text with the same id above, I must have put when I was doing some test. It worked blz, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.