Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
Problem with toggling display-type HTML JavaScript

I have a problem toggling between display: none; & display: block;

Here is the element i want to toggle:

.dd-content #myLinks{
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  padding: 12px 16px;
  z-index: 1;
}

JS:

<script>
function myFunction() {
  var x = document.getElementsById("myLinks");
  if (x.style.display === "block") {
    x.style.display = "none";
  } else {
    x.style.display = "block";
  }
}
</script>

Calling on that function:

<a href="javascript:void(0);" class="icon" onclick="myFunction()">Link</a>

What happens now is that the element is displayed as a block and the Link won't toggle it back...

What am i doing wrong?

fiddle: https://jsfiddle.net/kx37dcbv/

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There are two issues here:

  1. First, there is no such thing as getElementsById, instead, you should be calling getElementById.
  2. You don't actually have an element in your HTML code with the ID myLinks. In your fiddle, you have this block between lines 208-226:
<section class="p-menu1">
  <nav id="navbar" class="navigation" role="navigation">
     <input id="toggle1" type="checkbox" />
     <label class="hamburger1" for="toggle1">
        <div class="top"></div>
        <div class="meat"></div>
        <div class="bottom"></div>
     </label>
     <nav class="menu1">
        <div class="dd">
           <a href="javascript:void(0);" class="icon" onclick="myFunction()">Link</a>
           <div class="dd-content">
              <p>Hello World!</p>
           </div>
        </div>
     </nav>
  </nav>
</section>

When I run the fiddle and click on your 'Link', I get the error "<a class='gotoLine' href='#283:9'>283:9</a> Uncaught TypeError: Cannot read properties of null (reading 'style')". This tells me that your function call to getElementById is unable to find an element with that ID.

You need to add an id="myLinks" to one of your elements in your HTML. You currently have:

.dd-content #myLinks{
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  padding: 12px 16px;
  z-index: 1;
}

but this is just defining the styling for those elements, so for your div <div class="dd-content"> you need to change this to <div class="dd-content" id="myLinks"> in order for your function to be able to find the element identified by that ID.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda