Tips for utilizing div as a dropdown menu

I have a challenge with the following code and I am looking to make it function like a dropdown one by one

For example:

ABCD

ABCD
  1234

ABCD
  1234
    abcd

<ul class="first">
  <li class="first-a"><a href="https://someurl">ABCD</a>
    <ul class="second">
      <li class="second-a"><a href="https://someurl">1234</a>
        <ul class="third">
          <li class="third-a"><a href="https://someurl">abcd</a></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

I have implemented CSS along these lines:

.first li {
  display:inline-block;
  position: relative;
}

.second {
 display:none;
 position: absolute;
}
.second.open {
  display: block;
}

and added this JavaScript:

$( ".first-a" ).click(function() {
  $(".second").removeClass( "open" );
  $(".second", this).toggleClass( "open" );
});
$(window).click(function() {
 $(".second" ).removeClass( "open" );
});
$(".first-a").click(function(event){
    event.stopPropagation();
});

However, so far none of these methods have been successful in achieving the desired functionality

Answer №1

To add functionality to a specific li element within your project, you can create a click event using jQuery. First, give the li element a class for targeted selection. Then, remove the "open" class from all sibling li elements and toggle the class of the clicked li.

$("li").click(function(e) {
  e.preventDefault();        
  e.stopPropagation();       
  var $thisLi = $(this);     
  $thisLi.parent().children('li').not($thisLi).removeClass( "open");
  $thisLi.toggleClass( "open");                                      
});
li {
  position: relative;
}

li>ul {
  display:none;  
}

li.open>ul {
  display:block;  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="first">
  <li class="first-a"><a href="https://someurl">ABCD</a>
    <ul class="second">
      <li class="second-a"><a href="https://someurl">1234</a>
        <ul class="third">
          <li class="third-a"><a href="https://someurl">abcd</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li class="first-a"><a href="https://someurl">ABCD</a>
    <ul class="second">
      <li class="second-a"><a href="https://someurl">1234</a>
        <ul class="third">
          <li class="third-a"><a href="https://someurl">abcd</a></li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

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

JavaScript has been used to modify a cell's URL in jqGrid

Currently, I am utilizing struts2-jqgrid along with JavaScript. After the jqgrid has finished loading, it retrieves <s:url var="updateurl" action="pagosActualizar"/>. Subsequently, in the HTML view source, jqgrid generates options_gridtable.cellurl = ...

Select any menu option to return to the one-page layout and then scroll down to find the location

I'm wondering if there is a way to navigate back from an external page to a specific section on my one-page website with a fixed menu? On my one-pager website, I have a fixed menu that includes a "apply for a job" button. When clicked, it takes users ...

What is the proper sequence for binding jQuery to elements?

I'm currently facing a challenge with using jQuery to link a function to an element. Essentially, I'm loading a series of divs via JSON, with each item in the JSON having an "action" assigned to it that determines which function should be trigger ...

Is it more effective to specify the class within the selector when using jQuery to remove a class?

Does including the class in the selector when removing a class using jQuery have any impact on performance or best practices? I'm curious if there will be any noticeable difference. For example, do you include it like this: $('#myList li.classT ...

Chic and perfectly aligned stripe button design

I need assistance with centering and styling the Stripe checkout button labeled "update card." Additionally, I want to adjust the appearance of the card number input field to be normal but readonly. How can I achieve this without relying on Bootstrap' ...

Implementing icon display upon click in a Meteor application

Currently, I am in the process of developing an application using meteor and within one of the templates, I have the following code snippet. <h3> <b> <a class="viewed" href="/jobdetails/{{_id}}">{{title}}</a> </b> ...

Are the site-wide navigation links working on one page but not the other?

Currently, I am in the process of constructing a website on rails and have successfully integrated a site-wide navigation bar to display on all pages. However, an issue has arisen where the navbar is visible on both pages, yet the links and hover effects a ...

Retrieving form data from within the resource error callback scope

For my client-side validation on submit, I am calling a resource if the form is valid. On success, everything works fine. However, when encountering an error handler, I also perform server-side validation on my data transfer object bean which contains Hibe ...

Error: operand a contains an invalid 'in' statement

After sending a request to the server using jQuery's $.ajax, I am getting back JSON data. $.ajax({ url: 'moreMonth.ajax', data: { startIndex: id }, success: function(data) { $.each(data, function(k, v) { alert(k + ':&ap ...

Creating an animated Gif using HTML and CSS styling

I am trying to create an effect where a .gif animation plays once when the mouse hovers over a specific image. I have one static image in PNG format and one animated gif. The goal is for the gif to play every time the mouse is hovered over the png image. ...

What is the counterpart of Ajax.updater in Jquery?

Could you please advise on how to achieve the same functionality as the prototype code below but using Jquery? var myAjax = new Ajax.Updater('abc', '/billing/add_bill_detail', { method: 'get', parameters: pars, ...

Changing a class and audio and storing it using browser's local storage

The challenge I am facing: I am currently working on a feature for my website that allows users to toggle the volume on/off and have that setting persist across different pages. Specifically, I want the user's volume preference to be saved when they n ...

Determine the size of a file in either megabytes or kiloby

I need to determine the size of a file in either megabytes if the value is greater than 1024 or kilobytes if less than 1024. $(document).ready(function() { $('input[type="file"]').change(function(event) { var _size = this.files[0].si ...

Attaching a buoyant div to the precise location of a different element

I have a unordered list (ul) with individual list items (li) that are displayed within a scrollable container. This means that only 8 list items are visible at a time, but you can scroll through them to see the others. Each list item (li) has an "edit" b ...

jQuery - Designing a customizable slider for rating a product or service

Currently, I am in the process of creating a web page utilizing jQuery that requires users to rate something on a scale. This rating scale would involve users selecting a number between 0 and 1 to reflect their knowledge on a particular topic. Instead of a ...

Create a fully responsive introduction page with three columns using Bootstrap, ensuring that the height of the

Currently, I am in the process of designing a website for a restaurant, hotel, and vineyard. My goal is to create three separate websites that are linked through one introductory page. The concept for the intro page involves three columns displayed side b ...

Is there a way to locate ComputedStyle elements using Nokogiri?

I am currently using Ruby along with Nokogiri to parse through HTML documents. I am looking to select all nodes that match a CSS class with the style attribute display: none, but the CSS class is unknown in advance. For example: <html> <body&g ...

A customizable and adaptable Tetris-inspired CSS design perfect for any screen size

If we imagine having a block like this: <div class="block"></div> There are different sizes of the block: <div class="block b1x1"></div> <div class="block b2x1"></div> <div class="block b1x2"></div> For i ...

attempting to shift course, but finding no success

I'm attempting to include dir=rtl or direction: rtl in the CSS, but it doesn't seem to have any effect on the browser and the content still displays left to right. What steps can I take to fix this? This template is pre-made from Colorlib. It&ap ...

Store user input in a paragraph

I want to create a unique program that allows users to input text in a field, and when they click "Start", the text will appear in a paragraph backwards. I plan to use Html, jQuery, and CSS for this project. Can anyone provide guidance on how to achieve th ...