Unable to set the height for ul/div elements

I'm struggling to make my navbar appear when I click the "menu button". The div seems to be present in the code, but with a height of 0. Here's the relevant section of the code causing the issue. Any suggestions on how I can resolve this?

var onOff = 0;

function dropdown() {
    "use strict";
    var navbar = document.getElementById("nav_ul");
    var ulDiv = document.getElementById("ulDiv");
    if (onOff === 0) {
        navbar.setAttribute("class", "navShow");
        ulDiv.setAttribute("class", "navShow");
        onOff = 1;
    } else if (onOff === 1) {
        navbar.setAttribute("class", "navHide");
        ulDiv.setAttribute("class", "navHide");
        onOff = 0;
    }
}
#ulDiv{
    text-align: center;
    float: none;
}

ul{
    position: relative;
    bottom: 0;
    margin-left: auto;
    margin-right: auto;
    padding-bottom: 20px;
}
li {
    display: inline;
    padding-top: 0;
    margin-top: 0;
    padding-left: 20px;
    padding-right: 20px;
    padding-bottom: 5px;
}
.navHide{
    display: none;
}
.navShow{
    display: block;
    height:auto;
}
@media screen and (max-width: 1005px) {
    #ulDiv{
        height: auto;
    }
    
    #nav_ul{
        display: none;
        width: 100px;
        z-index: 3;
        position: absolute;
        top: 106px;
        left: 0px;
        margin: 0px;
        padding-bottom: 20px;
    }
    li{
        display: inline-block;
    }
}
<header>
        <nav id="navbar" class="navbar">
        <a href="home.html" id="logoA"><img id="logo" alt="logo" src="logo_dk.jpg"></a>
        <button id="menu-but" onclick="dropdown()"><img id="meny-icon" alt="meny icon" src="menu-icon.png"></button>
        <div id="ulDiv"><ul id="nav_ul" class="none">
        <li><a href="home.html">Home</a></li>
        <li><a href="#">Lawyers</a></li>
        <li><a href="#">Practice Areas</a></li>
        <li><a href="#">International</a></li>
        <li><a href="#">Information</a></li>
        <li><a href="#">Careers</a></li>
        <li><a href="#">History</a></li>
        <li><a href="#">Contact</a></li>
        </ul>
        </div>
        </nav>
    </header>

Answer №1

  var onOff = 0;

window.dropdown = function() {
    "use strict";
    var navbar = document.getElementById("nav_ul");
    var ulDiv = document.getElementById("ulDiv");
    if (onOff === 0) {
        navbar.setAttribute("class", "navShow");
        ulDiv.setAttribute("class", "navShow");
        onOff = 1;
    } else if (onOff === 1) {
        navbar.setAttribute("class", "navHide");
        ulDiv.setAttribute("class", "navHide");
        onOff = 0;
    }
}
#ulDiv{
    text-align: center;
    float: none;
    display: none;
}

ul{
    position: relative;
    bottom: 0;
    margin-left: auto;
    margin-right: auto;
    padding-bottom: 20px;
}
li {
    display: inline;
    padding-top: 0;
    margin-top: 0;
    padding-left: 20px;
    padding-right: 20px;
    padding-bottom: 5px;
}
header .navHide{
    display: none;
}
header .navShow{
    display: block !important;
    height:auto;
}
@media screen and (max-width: 1005px) {
    #ulDiv{
        height: auto;
    }

    #nav_ul{
     
        width: 100px;
        z-index: 3;
        position: absolute;
        top: 106px;
        left: 0px;
        margin: 0px;
        padding-bottom: 20px;
    }
    li{
        display: inline-block;
    }
}
<header>
        <nav id="navbar" class="navbar">
        <a href="home.html" id="logoA"><img id="logo" alt="logo" src="logo_dk.jpg"></a>
        <button id="menu-but" onclick="window.dropdown()"><img id="meny-icon" alt="meny icon" src="menu-icon.png"></button>
        <div id="ulDiv"><ul id="nav_ul" class="none">
        <li><a href="home.html">Forside</a></li>
        <li><a href="#">Advokater</a></li>
        <li><a href="#">Sagsområder</a></li>
        <li><a href="#">Internationalt</a></li>
        <li><a href="#">Oplysninger</a></li>
        <li><a href="#">Karriere</a></li>
        <li><a href="#">Historie</a></li>
        <li><a href="#">Kontakt</a></li>
        </ul>
        </div>
        </nav>
    </header>
    
    <script>
  
    </script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How should h tags be correctly utilized?

Is it better to use h tags or lists to style content? Take a look at this code, is using h tags the appropriate choice here or should I consider using a list instead? <div class="col-xm-12 col-sm-6 col-md-4"> <h4>Get in touch< ...

Executing polymorphism in Javascript without the use of OOP classes

In JavaScript or other object-oriented programming languages, polymorphism is achieved by creating different types. For instance: class Field {...} class DropdownField extends Field { getValue() { //implementation .... } } Imagine a library f ...

tips and tricks for adding content to json with the help of jquery/javascript

Is it possible to include text in a JSON array using jQuery or Javascript? Assuming that there is data in the following format: A list of numerical codes stored in a text file as 123, 456, 789 I am looking to convert them into a JSON array using JavaScr ...

Is it necessary to include a module in another module if it is not utilized in the template?

Is it necessary to import Module2 into Module1 if a component from Module2 is being used in Module1, but only in the typescript and not the template? For instance, as a @ContentChild(Component2) component2 like shown below (Note: both modules are secondary ...

How to initialize data in Vue.js components

I'm working on a Map.vue component and I need to update its data in Vue.js. However, I'm struggling to figure out how to do this: <template> <div> <div class="google-map" :id="mapName"> </div> </div> < ...

Is it true that Redux Saga's select function returns mutable state?

Is the state returned by Redux Saga's select function mutable or immutable? To learn more, visit https://github.com/redux-saga/redux-saga ...

Built-in Promises within MongoDB

Is there a way to determine which methods in mongoDb have an inbuilt promise? For example, "updateOne() , findOne()" have inbuilt promises that we can access using ".then", but many other mongoDB methods lack this feature. How can we identify which methods ...

Enabling automatic scaling during the rendering of an .stl file using three.js

I'm in the process of developing a server/webpage with the following functionality: Users can upload an .stl file to be 3D-printed The server executes a mesh repair script on the uploaded .stl file The corrected .stl file is displayed in the user&ap ...

External JS file not executing function in AJAX response (only runs when function is directly included in AJAX response)

My issue involves an AJAX response not executing a simple jQuery function that is located in an external JS file. The function only runs when its code is directly placed within the AJAX response view. The page containing a link for dynamically loading AJA ...

Unlock the power of VueJS with advanced checkbox toggling for array data

I have encountered an issue while working with VueJS regarding the implementation of two features for a set of checkboxes. Initially, the checkboxes are generated dynamically. I am in need of a master 'toggle' checkbox that can toggle the stat ...

What is the process for assigning default values to dynamically generated form elements?

I have been working on building a single-page application (SPA) using JavaScript and jQuery. During the process, I encountered an issue with a dynamic form. The user is able to add specific fields as needed, but if they don't generate and utilize all ...

Using JavaScript, the list of items (images) will be displayed and placed into HTML panels

Below is the layout structure on my website: <div class="panel-heading"><h3 class="panel-title">Suggestions</h3></div> <div class="panel-body"> <div class="col-md-7"> <h3><span class= ...

The display: table attribute is proving to be ineffective in creating the desired table layout. I am seeking alternative ways to

Is there a way to create a table using the CSS property display: table without utilizing the table element? I've searched for examples, but haven't found anything that has been helpful. Can someone please provide a fiddle of a table with a minimu ...

Protractor - selecting a hyperlink from a list

Imagine you have a todo application with tasks listed as follows: Walk the dog, Eat lunch, Go shopping. Each task has an associated 'complete' link. If you are using Protractor, how can you click on the 'complete' link for the second t ...

Guide on setting up a configuration node in Node-RED

I am attempting to create a config node similar to this example, but it only displays a text box and not the configuration options. I want the projectid to be a config node, and despite trying various nodes with config setups, nothing seems to work for me. ...

Determine which scroll bar is currently in use

I'm running into an issue with multiple scrollbars on my page - they just don't seem to be functioning correctly: <div class="dates-container" v-for="id in ids"> <overlay-scrollbars :ref="`datesHeader` ...

Utilize dynamic Google Maps markers in Angular by incorporating HTTP requests

Struggling to find a solution for this issue, but it seems like it should be simple enough. Initially, I fetch my JSON data using the code snippet below: testApp.controller("cat0Controller", function($scope, $http){ var url = "../../../Data/JSONs/randomd ...

Sending JSON-encoded data using HTML5 Server-Sent Events (SSE) is a

I have a script that triggers an SSE event to fetch json encoded data from online.php. After some research, I discovered methods for sending JSON data via SSE by adding line breaks. My question is how to send JSON through SSE when the JSON array is genera ...

Angular 2: Applying class to td element when clicked

I am working with a table structured like this <table> <tbody> <tr *ngFor="let row of createRange(seats.theatreDimension.rowNum)"> <td [ngClass]="{'reserved': isReserved(row, seat)}" id={{row}}_{{sea ...

We encountered an issue loading the resource: the server has returned a 404 (Not Found) error message and a 403 Forbidden error message. However, the resource functions properly on Codepen and localhost

I have a CodePen demo that works fine. However, when I upload it to the server, it stops working properly and most external references show 404 errors. Initially, there were no problems, but now it seems to be an issue like this: !--> http://sachin.ipov ...