develop an interactive dropdown selection feature

Can someone please guide me on how to create a dropdown menu that activates with a click instead of hover? I want the menu to appear when clicked and disappear when clicking outside of it, similar to the Facebook news feed option. Below is my code:

<head>
<style type="text/css">
#menu{margin:0; padding:0}
#menu ul{margin:0; padding:0; line-height:20px;}/*dropdown*/
#menu li{margin:0; padding:0; list-style:none;float:left; position:relative;}
#menu ul li a{text-align:center; fot-family:"arial"; text-docoration-none;      height:30px; width:150px; display:block; color:#333fff}
#menu ul ul{position:absolute;visibility:hidden}
#menu ul li:hover ul{visibility:visible}
/*---highlight list has i rollover it--*/
#menu  ul li:hover ul li:hover {background:#ccc}
</style>
</head>


<div id="wrapper" style="backgroud-color:blue">

<div id="menu">
<ul >
<li><a href="#" onclick="return false;">Drop</a>
<ul>
<li><a href="#">link 1</a>
<li><a href="#">link 2</a>
<li><a href="#">link 3</a>
</ul><!--end inner ul -->
</li><!--end main li -->
</ul><!--end main ul -->
</div><!--end menu ul -->
</div>
<div></div>

Answer №1

UPDATE: Made changes to the jsFiddle ...

To achieve this, you will require some JavaScript code. Check out the jsfiddle link: here

In essence:

Your HTML structure

<div id="wrapper" style="backgroud-color:blue">

  <div id="menu">
    <ul >
      <li>
        <a href="#" id="drop-menu" >Drop</a>
        <ul id="menu1">
          <li><a href="#">link 1</a>
          <li><a href="#">link 2</a>
          <li><a href="#">link 3</a>
        </ul><!--end inner ul -->
      </li><!--end main li -->
    </ul><!--end main ul -->
  </div><!--end menu ul -->
</div>

Your CSS styling

#menu{margin:0; padding:0}
#menu ul{margin:0; padding:0; line-height:20px;} /*dropdown*/
#menu li{margin:0; padding:0; list-style:none;float:left; position:relative;}
#menu  ul li a{text-align:center; font-family:"arial"; text-decoration:none; height:30px; width:150px; display:block; color:#333fff}
#menu ul ul{position:absolute;visibility:hidden}

/*---highlight list on hover--*/
#menu ul li:hover ul li:hover {background:#ccc}

.visible {
    visibility: visible !important;
}

Your jQuery script

$(function(){
    $('#drop-menu').bind('click', function(){
         $('#menu1').toggleClass('visible');
    });
});

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

CSS transitioning can be tricky when trying to fade an image or video with a solid background alongside an HTML element that shares the same background color

When attempting to apply a fade in/out effect to an image or video with a solid background color and using the same transition for the opacity of an html element with a matching background color, a noticeable "square" appears around the image/video. Why d ...

When a textbox is modified and a button is clicked in ASP.NET, triggering both client-side and server-side events

I have a popup that allows users to change an address. It contains three text boxes (Address, Province, and ZIP) and one DropDownList for the City. When the ZIP code is changed, an ajax call updates the province text box. I also want to later populate the ...

Prevent unexpected page breaks in <tr> elements on Firefox (Could JavaScript be the solution?)

Wondering if there are any ways, possibly through JavaScript or CSS, to avoid having page breaks within <tr> elements specifically in Firefox. Given that FF does not yet fully support page-break-inside, I assume this might need to be addressed using ...

Validation using jQuery for date counting

I recently started utilizing the jQuery form validation plugin from here. However, I encountered a challenge. I have three select boxes for D.O.B [Day], [Month], and [Year]. My goal is to determine if the user is older than 13 years old. Unfortunately, I ...

Are dots mysteriously showing up in the navigation bar on Firefox?

Recently, I created a navigation bar on Chrome and everything seems to be working fine. However, when I tested it on Firefox, I noticed that there is an unexpected dot appearing in front of each navigation tab. Does anyone have any idea why this might be h ...

How can I retrieve the value from an input form using jQuery if it returns null?

Despite trying various methods, I have been unsuccessful in retrieving form values using jQuery and sending them in an AJAX GET request. The value keeps showing as null even after spending more than 18 hours on it. Any help would be greatly appreciated. $ ...

Using Material-UI with React to solve the issue of DatePicker being hidden under modal

The MUI Parent Component includes a Page, followed by a modal also from MUI. Inside the modal, I am rendering the DatePicker component. // @mui import { DatePicker } from '@mui/x-date-pickers'; This is how it appears on the screen. <Contr ...

Enhance hyperlink with Ajax functionality to display content upon success; if unsuccessful, redirect by default

My webpage features a <a href> hyperlink that, when clicked, uses JQuery to load contents into a div on the same page. This functionality is working correctly. However, I want the link to revert back to its original behavior and go to the href url i ...

What is the best way to adjust the canvas size within a list item?

I have successfully created a 3D model with Zdog that dynamically resizes based on the screen size. The model includes a dome and brim, which rotate smoothly as intended. Here is the code snippet: const TAU = Zdog.TAU; let hat = new Zdog.Illustration({ ...

The functionality of JQuery's `.on("click"... is sporadically functioning

I have a code block that dynamically generates elements and includes event handling. However, the event handling sometimes works and other times it doesn't. I'm not sure how to debug this issue. Can someone help me figure out what might be causin ...

Divs that are floated and only partially visible at the end

My layout consists of 6 Divs floated left to create 6 columns. The width of all these floats combined may extend beyond the window width for most users, especially with the 6th column included. Is there a way for this 6th column to be partially visible ( ...

The issue with the jQuery Mobile onclick event is that it fails to remove the ui-disabled

Ever since I delved into coding with jQuery Mobile, I've noticed a change in the function that once effortlessly removed the "ui-disabled" class. Now, all it does is display an alert message... Snippet from the HTML code: <div data-role="navbar ...

Index.php triggering a blank pop-up on the website's live platform

After successfully running my default page, index.php, on xampp, I decided to upload my entire site to 1&1, my domain and hosting provider. However, upon visiting my domain, I encountered an empty alert with no message and a blank page. To troubleshoo ...

Trouble with AJAX GET request functionality

I am new to AJAX and attempting to make my first AJAX call. Here is the code I have so far: $.get( "validate.php", { 'userinput':'x'}, function(response) { if( response.status ) alert( "Matches found" ); else alert( "No matches ...

"What is the best approach to adjusting the width of one div based on the width of another div using CSS Grid

As a beginner in programming, I'm trying to work with CSS Grid but facing some challenges. My goal is to create a component with two columns: the left column should have a width of minmax(570px, 720px), and the right column should be minmax(380px, 10 ...

Executing a jQuery AJAX request within a script that has been imported from a different domain

Yes, I understand the concept of the same domain policy and how JQuery can work around it using JSONP. However, I am facing a slightly different issue. Browsers typically restrict cross-domain content calls due to the "same domain policy." That makes se ...

Accessing JavaScript elements from PHP code

Is it possible to recognize JavaScript elements if they are nested within PHP tags? For example: <?php $username = "maniacal"; echo "<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>" ?> That's ...

Display and conceal information upon tweeting

Can anyone help me troubleshoot an issue with hiding the Twitter button after displaying new content? I tried adding a script to make it work, but it's still not functioning properly. Any insights on what I might be doing wrong would be greatly apprec ...

Turn the flipcard hover effect into an onclick action

After successfully creating interactive flip cards using CSS hover effects, I am now faced with the challenge of adapting them for mobile devices. I'm struggling to figure out how to translate my hover effects into onclick events for a mobile-friendl ...

Updating the Progress Bar in Shopify when the "Add to Cart" button is clicked: A step-by

I'm currently working on implementing a shipping progress bar on my Shopify Theme. However, I've encountered an issue where the progress bar does not update correctly unless I refresh the page along with the content text. Can anyone provide guid ...