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

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

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 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!