Option tag for a sub-menu

I'm currently working on an application that requires a submenu within the option tag. The idea is that when a user clicks on one of the submenu options, it should appear in the search bar for searching purposes. Unfortunately, I'm facing some challenges with implementing this feature. Below is the code snippet that I've been working with:

<div class="form-row">
            <select class="form-control mx-sm-3"">
            <option selected="selected">Variant</option>
            <option>Protein Variant</option>
            <option>Copy Number Alterations</option>
            <ul class="nav navbar-nav">
                <li class="dropdown">
                    <option href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true"> <span class="nav-label">Services</span> <span class="caret"></span></option>
                    <ul class="dropdown-menu">
                        <option>Service A</option>
                        <option>Service B</option>
                        <option>Service C</option>
                    </ul>
                </li>
            </ul>
            </select>
 </div>

The expected behavior is that when the user hovers over 'service', a new box should display with options 'Service A', 'Service B', 'Service C'.

Answer №1

Only using HTML allows for the optgroup feature to be implemented:

<select>
  <optgroup label="Swedish Cars">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select> 

If a more specific dropdown is needed, JavaScript can be used for implementation, like shown below:

$(document).ready(function() {
  $('.dropdown-submenu a').on("click", function(e) {
    $(this).parents('ul').find('.dropdown-submenu ul').hide();
    $(this).next('ul').toggle();
    e.stopPropagation();
    e.preventDefault();
  });
});
.dropdown-submenu {
  position: relative;
}

.dropdown-submenu .dropdown-menu {
  top: 0;
  left: 100%;
  margin-top: -1px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    All South Africa
  </button>
  <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <li class="dropdown-submenu">
      <a class="dropdown-item" href="#">Variant</a>
      <a class="dropdown-item" href="#">Protein Variant</a>
      <a class="dropdown-item" href="#">Copy Number Alterations</a>
      <ul class="dropdown-menu">
        <li><a class="dropdown-item" href="#">Service A</a></li>
        <li><a class="dropdown-item" href="#">Service B</a></li>
        <li><a class="dropdown-item" href="#">Service C</a></li>
      </ul>
    </li>
  </ul>
</div>

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

Prevent dropdown from closing when clicking inside components [react-bootstrap]

I am searching for a solution to hide a drop-down bot by clicking on elements inside it. I am exploring different methods using JQuery. Is it possible to achieve this using JavaScript in combination with react-bootstrap? My current approach involves calli ...

Can Socket.IO link to a source tag that doesn't actually exist?

As I was following the 'Get started thing' tutorial on Socket.IO, I encountered a step that required me to add the socket.io.js script to my HTML page. The instruction provided was: /socket.io/socket.io.js However, when I checked my folders, I ...

Is it feasible to add to an ID using ngx-bootstrap's dropdown feature?

In the documentation for ngx dropdown, there is a feature called "append to body." I recently tried changing this to append to a table element instead and it worked successfully. Now, on another page, I have two tables displayed. If I were to assign each ...

Canvas.js graph failing to refresh with updated data

I have encountered an issue while using CanvasJS to generate a line graph with data from Myo. The problem is that when new data is received, the graph fails to update. This may be due to the fact that I did not initially populate the graph with any data po ...

Fixed background & cover design for Internet Explorer 11

Visit this link for more information: http://apolytos.com/new/img/test.html <!doctype html> <html> <head> <meta charset="utf-8"> <style> body { background:url("background.jpg"); background-repeat:no-repeat; backgr ...

Exploring the characteristics of images in Javascript

I've gone through my code multiple times but I just can't figure out why it's not functioning correctly... Could someone shed some light on this? What am I missing here? The purpose of the code is to allow users to input different values i ...

Arranging images and their descriptions side by side in CSS and HTML

I am facing an issue with a row containing 5 images and descriptions below them. If the descriptions do not have the same width, the images do not stay on the same row anymore; they go up and down. Here is my current setup: https://i.sstatic.net/c9smB.pn ...

Adjust the position of the content to the bottom for a form-group in Bootstrap v4

Currently, I am facing an issue where my label is overflowing and wrapping around the input field, causing it not to align at the bottom of the div. Is there a straightforward CSS solution for this problem? I have experimented with position relative/absol ...

Ways to address the issue of "$ is not a function"

Whenever I attempt to upload an image, this error message pops up: $ is not a function The source of the error can be found here: $(document).height(); ...

Centering a dynamic icon within the grid placement

Is there a way to center an icon on a page within a responsive grid layout with multiple items? I have tried using absolute positioning for the icon but it seems to be taking into account the entire screen's range of motion instead of just staying wit ...

What is causing the content to overflow outside of the navbar on smaller screens or mobile devices?

/* *{ font-family: sans-serif; box-sizing: border-box; margin: 0; padding: 0; overflow-x: hidden; } */ .main { min-height: calc(100vh - 10rem); display: grid; place-items: center; } p { font-size: 4rem; line-height: 1.6; } nav ...

What is the most streamlined way to send data from HTML forms using solely JavaScript?

I currently employ jQuery to transmit data from my HTML forms and then I utilize PHP to save it in my database. Below is the code snippet that I am using: HTML FORM: <form id="formpost" action="" method="post" onsubmit="return false"> <input t ...

Incorporating a vertical slider alongside a fullPage.js section for a dynamic user experience

Is there a way to achieve a transition effect similar to this example for fullPage.js section-elements? <div id="fullpage"> <div class="section"></div> <div class="section"></div> </div> I have experimented with var ...

I require varying numbers of columns on each row based on the breakpoint in Bootstrap4

After realizing it was easier than expected, I simply consolidated all columns into one row and the responsiveness now works perfectly. Thank you to everyone who helped. https://i.sstatic.net/4Sx41.png I am utilizing Bootstrap 4 grid system to structure 3 ...

Utilizing CSS to incorporate a background image into HTML code

Is it feasible to utilize pure HTML for the background-image property? Consider the following scenario: The original: background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height=&apo ...

How to implement scroll spy in Bootstrap 4 to highlight parent li elements?

I have a bootstrap4 menu set up like this: <ul class="navbar-nav ml-auto"> <li class="nav-item"><a class="nav-link" href="#introduction">INTRODUCTION <span class="sr-only">(current)</span></a></li> </ul> Th ...

Moves are chosen to move downward when positioned next to an inline-block level element

Check out this jsfiddle example How can I prevent selects from moving down when there is an inline-block level element next to it? <select> <option value="" >Day</option> </select><select> <option value="" >Mon ...

Creating a blank grid using ng-repeat in angularJS

I'm attempting to create an empty grid using angularJS, but I've only been working with it for 2 days so I'm not very experienced. This is what I have come up with so far: <!doctype html> <html ng-app> <head> < ...

Display website when clicked

When creating a website similar to , one issue that I am facing is the ability to scroll down before clicking the "proceed as anticipated" button. This feature seems to defeat the purpose of having the button trigger an automated scrolling effect if users ...

Steps for modifying the CSS class of an <a> tag with Jquery/Javascript

I am attempting to dynamically change the class of a tab on the dashboard based on the selected page. In the dashboard, there are 3 tabs: <div> <ul class="menu"> <li class="MenuDashboard"><a href="#" >Dashboard</a&g ...