I am unable to determine if I have already selected a List Item

My goal is to have a functionality where clicking on "Download Drivers" will open the list, and clicking again will close it. This should be achieved with onclick events only, no hover effects. Additionally, I want the list to remain open even if I click on one of the items within the list.

This is my HTML structure:

<ul class="dropdown">
    <li><a id="drivers" onclick="dropdown(this); return false;" class="dir">Download Drivers</a>
        <ul id="driversmenu" class="sub">
            <li><a>Download Drivers</a></li>
            <li><a>Driver Widget</a></li>
            <li><a>NVIDIA Software</a></li>
        </ul>
    </li>
    </ul>

Here's the Javascript code snippet:

   function dropdown(obj)
   {
       alert("klik");
       var elements = document.getElementsByClassName("sub");
       var ids = '';
       for (var i = 0; i < elements.length; i++)
       {
           document.getElementById(elements[i].id).style.display = 'none';
       }

       if (document.getElementById(obj.id + 'menu').style.display == 'block')
       {
           alert("al open");
           document.getElementById(obj.id + 'menu').style.display = 'hidden';
       }
       else if (document.getElementById(obj.id + 'menu').style.display = 'hidden')
       {
           alert("niet open");

           document.getElementById(obj.id + 'menu').style.display = 'block';
       }
       return false;
   };

The CSS styles are included below, although their relevance is uncertain:

ul.dropdown {    list-style-type: none; width: 300px;float: left; height: 0px; margin-top: 0px; padding-left: 0px; }
ul.dropdown a { display: block; color: white; text-decoration: none;     list-style-type: none; }
ul.dropdown li {  float: left; padding: 0 0px; border: 1px dotted #d0d0d0;     list-style-type: none;}
ul.dropdown li ul { display: none; position: absolute; z-index: 99; }
ul.dropdown li ul li { clear: both; margin-left: 0px; padding-left: 0px; border: 0; min-width: 150px;  }
ul.dropdown li ul li:hover { background-color: #d1d1d1; }
ul.dropdown li ul li:hover a { color: red; }

I'm encountering an issue where the condition never triggers this part of the code:

if (document.getElementById(obj.id + 'menu').style.display == 'block')
{
    alert("al open");
    document.getElementById(obj.id + 'menu').style.display = 'hidden';
}

If anyone can provide insights into what might be going wrong, I would greatly appreciate it. Thank you in advance.

Answer №1

Attempt

function toggleDropdown(obj)
{
    var elements = document.getElementsByClassName("sub");
    var ids = '', display = document.getElementById(obj.id+'menu').style.display;
    for(var i=0; i < elements.length; i++) {
        elements[i].style.display='none';
    }
    if(!display || display=='none'){
        document.getElementById(obj.id+'menu').style.display='block';
    }
    return false;
}; 

View Demo: Fiddle

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

Tips for duplicating a collada model in three.js?

I've imported a .dae model into my scene and I want to use it multiple times. The code provided works with meshes, but the collada.scene object is not a mesh: var mesh2 = new THREE.Mesh( loadedMesh.geometry, loadedMesh.material ); Is there a way to ...

Tips for aligning numbers in a monospaced ordered list

Is there a way to align the numbers in a numbered list within a table using monospace font so that they are at the same level as bullets on a bullet list? Simplest example: ol { font-family: monospace; } ul, ol { margin-top: 10px; margin-bottom: ...

Transforming images with Imagick

I've been trying to generate thumbnails from PDF uploads using Imagick. I have a script that is supposed to handle this task, but unfortunately, it only uploads the file without creating a thumbnail. I know some of you may find this basic, but PHP is ...

Perform a toggle action on the first row when clicking within the current row using Jquery

I've been grappling with the idea of creating a function that can display and hide a comment field when a button is clicked. The challenge here is that there are multiple line items with their own comment boxes. I want to find a way to achieve this wi ...

The issue with the Woocommerce quantity increment buttons not functioning properly persists after an AJAX refresh, and the automatic load feature only activates after two

I've hit a brick wall with this particular issue. Many people have suggested solutions, but none seem to be effective for me. My situation probably resonates with quite a few individuals: I decided to customize the WooCommerce quantity input (/global ...

"Rotating the TransformControl in threejs: A step-by-step guide

When the object attached to the transform control rotates, I want the transform control itself to rotate as well. Before the rotation: https://i.sstatic.net/yjTue.png After the rotation: https://i.sstatic.net/2opuU.png As shown in the image, before th ...

I wish to adjust the font size as well as resize the table elements simultaneously

Adjusting the height and width of the table should automatically adjust the font size as well. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Resizable - Default functiona ...

Tips for displaying numbers in thousands, millions, or billions in vue.js

For example: 12000 can be shown as 12k, 1000000 as 1m, and 1430 as 1.4k. <div v-for="item in items" :key="item"> <span>{{item.num}}</span> </div> <script lang='ts'> items:any[]=[ {num:"122256879"}, ...

Retrieving a single object from an array in node.js utilizing elemMatch

My goal is to extract a single object from an array of objects in a data collection using elemMatch. Here is the sample data: [ { "_id": "5ba10e24e1e9f4062801ddeb", "user": { "_id": "5b9b9097650c3414ac96bacc", "firstName": "blah", ...

Configuring routes for Angular4 router is a vital step in creating a

Issue: I am currently setting up routes for my application, aiming to structure the URL as https://localhost:4200/hero=id, where the 'id' will be dynamically selected. However, this setup is not functioning as expected. If I attempt to use a URL ...

How can I send form data using libcurl in C++ for a particular form?

How can I post and submit form data using libcurl and C++ for the form below? <form id="loginContainer" class="controllerContainer" action="/j_spring_security_check" method="post"> <h2>Login</h2> <hr> <fieldset> <div ...

How can I efficiently locate identical sequences of cells in two or more arrays?

Unique Example 1 We can explore an interesting scenario by considering two arrays: ('m','o','o','n','s','t','a','r','d') ('s','t','a', ...

The chosen choice is not able to be shown within the option tag

When attempting to filter options using JavaScript based on a selected item, I'm encountering issues. If the selected element contains 'CONTINUE' or 'BRACKET', it should be split into an array by space and only the first two indice ...

Understanding the Code for Responsive Web Typography with line-height in ems

I am currently trying to delve into the CSS calculation process behind the styling in the code snippet provided. This code snippet is from a responsive WordPress theme, and I am struggling to decipher how the em values for the line-height of the <h> ...

Creating geometric designs on an image and storing them using PHP

I am attempting to retrieve an image from a specific location on my server, draw lines on it, and then save the modified image back to the same location. Below is the code I am using: function drawShapes($src_path, $json) { echo "---inside dra ...

What are the steps to create two adaptable blocks and one immovable block?

Check out this HTML code snippet: <div style="border: 1px solid #000; width: 700px; line-height: 38px; display: block"> <div style="margin-left:10px;width: 222px; float: left; margin-right: 10px;"> Enter your question </div& ...

Toggle the on and off functionality of a button using ajax response for content display

I'm struggling to figure out why the button isn't working for me. I am familiar with how to enable and disable buttons using attr and prop. $(document).on("click", "#btn", function(){ $(this).html("Sending..."); $(this).prop("disabl ...

"Browser compatibility issues: 404 error on post request in Firefox, while request is

When following this tutorial on React and PostgreSQL, the app should display the JSON fetch in the bash terminal around the 37-minute mark. However, there seems to be a problem as there is no feedback showing up on the npm or nodemon servers. After tryin ...

PHP mail function does not properly align HTML table outputs

After fetching data from the database and inserting it into an HTML table, everything appears fine. However, upon pressing the send button to email the content, sometimes the alignment of the HTML table is disrupted in the email. It seems like specific col ...

Is it possible to eliminate the input border within React-Bootstrap?

Struggling to remove the input borders of the Form.Control component in React-Bootstrap. Despite searching for answers, nothing has been able to solve this issue. I've attempted redefining the .css classes "input" and "form-control", trying to set th ...