0

I want to have a very simple navigation menu. It has to be clickable. So when using this code

var isActive = true;

function toggleMenu(){
    var content = $("#navContent");

    isActive = !isActive; // toggle the current menu state
    if(isActive) {
        content.display = "block"; // show the items
    } else {
        content.display = "none"; // hide all items
        content.position = "absolute";
        content.zIndex = 1;
    }
}
#navContainer {
    position: relative;
    display: inline-block;
}

#navContent button {
    display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body onLoad="toggleMenu()">

<div id="navContainer">
    <button id="navBtn" onclick="toggleMenu()">Menu</button>
    <div id="navContent">
        <button>1</button>
        <button>2</button>
        <button>3</button>
        <button>4</button>
    </div>
</div>

</body>

and pressing on the menu button, I get into the toggleMenu() function but the items don't hide.

Can someone help me out?

3
  • Is the function being fired? Commented Aug 14, 2017 at 12:09
  • $("#navContent") returns a jQuery object, but to directly access .style you would need the reference to a DOM element. But jQuery has methods to manipulate element styles, so there is not need to mix these two approaches in the first place. It has .show/.hide as well (although these are not the ideal way to do this either - you should rather just toggle a class, and leave formatting to your stylesheet.) Commented Aug 14, 2017 at 12:10
  • Not sure why you don't just use toggle() for this and set the display to hidden in css to hide the menu upon page rendering but if you want to continue with the method you're using then you should be targeting the css before trying to target and change a css property. Example: content.css("display", "block"); Commented Aug 14, 2017 at 12:15

3 Answers 3

3

Your code is a rather odd mix of plain JS and jQuery. I'd suggest using one or the other. Here's a simple version of your code using jQuery:

$(function() {
  $('#navBtn').click(function() {
    $('#navContent').toggle();
  }); 
});
#navContainer {
  position: relative;
  display: inline-block;
}

#navContent button {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="navContainer">
  <button id="navBtn">Menu</button>
  <div id="navContent">
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

ah, element.toggle() keeps the code very clean :)
0

It would be much easier to make a CSS class that hides the elements, which is then toggled by JS. This answer doesn't require jQuery

function toggleMenu(){
    document.getElementById('navContent').classList.toggle("hidden")
}
#navContainer {
    position: relative;
    display: inline-block;
}

#navContent button {
    display: block;
}

.hidden {
    display: none;
}
<div id="navContainer">
    <button id="navBtn" onclick="toggleMenu()">Menu</button>
    <div id="navContent" class="hidden">
        <button>1</button>
        <button>2</button>
        <button>3</button>
        <button>4</button>
    </div>
</div>

Comments

0

Why not simplify it to:

$("#navBtn").click(function() {
  $("#navContent").toggle();
});
#navContainer {
  position: relative;
  display: inline-block;
}

#navContent {
  display: none;
}

#navContent button {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

  <div id="navContainer">
    <button id="navBtn">Menu</button>
    <div id="navContent">
      <button>1</button>
      <button>2</button>
      <button>3</button>
      <button>4</button>
    </div>
  </div>

</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.