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

195
Visualizações
how can I alter my code to hide element on load?

I'm using this js code to show/hide elements on my site:

<script>
    function myFunction3() {
  var x = document.getElementById("dsec-three");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>

Can anyone help me to set it so the elements are hidden on load and only show once activated?

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

0

you can use class for show/hide elements same as :

function showHide(){
  element = document.getElementById('element1');
  showHideEle(element)
}

function showHideMulti(){
  elements = document.getElementsByClassName('multi');
  Array.from(elements).forEach(ele => {
    showHideEle(ele)
  });
}

function showHideEle(ele){
  let isHide = ele.classList.contains('hide');
    if (isHide) {
      ele.classList.remove("hide");
    } else {
      ele.classList.add("hide");
    }
}
.hide {
  display: none
}
<p class="hide" id="element1">para 1</p>
<button onclick="showHide()">Show/Hide</button>
<br />
<p class="hide multi">para 2</p>
<p class="hide multi">para 3</p>
<p class="hide multi">para 4</p>


<button onclick="showHideMulti()">Show/Hide Multi</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Your function myFunction3 already does the job of hiding and showing the relevant elements. All you need to do now is invoke the function when your document loads. You can do this by adding the following line inside your <script>:

document.addEventListener("load", myFunction3);

If you need the elements to stay hidden by default and show them after the document loads, you'll need to set the display property of those elements to none either via inline CSS.

Example (if your element is a div) :

  <div id="dsec-three" style="display:none">
  ...
  </div>
about 4 years ago · Juan Pablo Isaza Relatório

0

what do you mean by activated?

if you mean that after the load of your page, you would need an event listener.

follow these steps:

  1. set the initial display of your element to none.

  2. create an event listener like load: and add your function there:

    document.addEventListerer('load', function(){ myFunction3(); })

the above code is telling the browser: 'when ever the page loads, call this function'

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