Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

192
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!