Trouble keeping the dropdown submenu from staying open

Trying to figure out how to keep my dropdown menu open only when clicked. Currently, the submenu disappears if I move my mouse away from it.

I attempted using e.stopPropagation without success. Is there a CSS or Javascript solution that can help with this issue?

HTML

<div class="container">
    <ul class="nav navbar-nav" id="myUlList">
        <li id="search-box" class="dropdown dropdown-search">
            <a class="menu-anchor" href="javascript:;">
            <i class="dropdown-search-icon glyphicon icon-search2"></i> Search Grants</a> <i class="dropdown-toggle" data-toggle="dropdown"></i>
            <div class="dropdown-menu" id="myDropDown">
                <div class="row">
                    <div class="col-xs-6 col-md-4">
                        <label for="activityCode">Activity Code</label>
                        <input name="activityCode" id="activityCode" class="form-control" type="text" maxlength="3" value={this.state.activityCode} onChange={this.handleValidateChange} />
                    </div>
                    <div class="col-xs-6 col-md-4">
                        <label for="awardId">Grant Number</label>
                        <input name="awardId" id="awardId" class="form-control" type="text" maxlength="10" value={this.state.awardId} onChange={this.handleValidateChange} />
                    </div>
                    <div class="col-xs-6 col-md-4">
                        <label for="granteeName">Grantee Name</label>
                        <input name="granteeName" id="granteeName" class="form-control" type="text" value={this.state.granteeName} onChange={this.handleChange} />
                    </div>                                            
                </div>
            </div>
        </li>     
    </ul>
</div>

JS

$('#myUlList').on({
    "click":function(e){
        e.stopPropagation();
    }
});
$('#myDropdown').on({
    "click": function (e) {
       e.stopPropagation();
    }
});

Answer №1

Implement the Bootstrap dropdown menu by using

<a class="dropdown-toggle" data-toggle="dropdown" href="#">
. No JQuery code is necessary for the dropdown functionality, but make sure to include jquery.min.js and bootstrap.min.js in your project.

<div class="container">
  <ul class="nav navbar-nav" id="myUlList">
    <li id="search-box" class="dropdown dropdown-search">
      <a class="dropdown-toggle menu-anchor" data-toggle="dropdown" href="#">
         <i class="dropdown-search-icon glyphicon icon-search2"></i> Search Grants
      </a>
      <div id="myDropDown" class="dropdown-menu">
        ...
      </div>
    </li>
  </ul>
</div>

Answer №2

Your jQuery syntax seems to be incorrect. Here's a suggestion on how to trigger a function when clicked:

$('#myUlList').on("click", function(e) {
        e.stopPropagation();
    });
$('#myDropdown').on("click", function(e) {
        e.stopPropagation();
    });

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

Performing a Javascript validation to ensure that a value falls within

How can I check if an input number falls within a specified range? I need to display if it is within the range, higher, or lower than the acceptable range. My current Javascript code seems to be causing an error message stating it is an invalid entry. It ...

Video margins within a webview

After numerous attempts to embed a YouTube video in a WebView, I'm still struggling with a persistent small margin on the right side of the video. I've researched similar issues and attempted to address it through JavaScript adjustments without ...

Node JS server fails to load CSS even with the use of express.static

It's quite absurd, but I've been grappling with a rather nonsensical code conundrum for days now. I just can't seem to get the CSS and image to load on my Node.js server. I've tried various solutions (like express.static, etc.), but n ...

Leveraging the amplify/reduce function with radio button selection

Please select from the following options: Using the up/down buttons in the main input should adjust the value accordingly. Clicking on any of the radio buttons below will update the price with the selected value. .priceBlock { width: 80%; border-r ...

jQuery Mobile using the old value instead of the new one

Sending a variable through a link: <a href="foo.php?var=101"> php </a> <a href="foo.php?var=102"> html </a> <a href="foo.php?var=102"> css </a> Upon reaching foo.php: <!-- this part is under data-role="page" !--> ...

Using jquery/offset to position a div causes it to float, but it does not take

<script type="text/javascript"> $(document).ready(function () { var top = $('#rt_outer').offset().top - parseFloat($('#rt_outer').css('marginTop').replace(/auto/, 0)); $(window).scroll(function (event) { // det ...

Attempting to toggle variable to true upon click, but encountering unexpected behavior

On my webpage, I have implemented a simple tab system that is only displayed when the variable disable_function is set to false. However, I am facing an issue with setting disable_function to true at the end of the page using a trigger. When this trigger ...

Reduce XSLTProcessor output by 50%

I have a persistent problem (from my perspective, at least). Every time I use XSLTProcessor.transformToFragment, the output is consistently halved compared to the input. For example, when I receive 200 entries in an XML file as a response from a webservi ...

How do I initiate PUT and DELETE requests from my HTML code?

I am currently in the process of developing a web application that will manage items within a list. Previously, I used buttons with event listeners and JavaScript functions to handle editing and deleting items. However, I am now transitioning towards build ...

Limiting the size of images within a specific section using CSS

When using CSS, I know how to resize images with the code snippets below: img {width:100%; height: auto; } img {max-width: 600px} While this method works effectively, it applies to every image on the page. What I really need is for some images to be ...

Aggregate values through an onclick event using a get request and iterate through each using

I am struggling to accumulate values obtained from a json get request using jQuery. The problem is that the values keep getting replaced instead of being added up in the variable total. Can someone please guide me on how to properly sum up the values with ...

Using Typescript with Material UI Select

I have implemented a dropdown menu using Material UI select labeled "Search By." When the menu is clicked, it displays a list of options. I aim to store the selected option and update the label "Search By" with the chosen option. export default function U ...

The AJAX requests in my project are failing to trigger upon touch events when using the jQuery Plugin

Ensuring that both <script src="hammer.js"></script> and <script src="../jquery.hammer.js-master/jquery.hammer.js"></script> have been correctly included in my header. I have a mouse click-triggered AJAX event for dynamic and depen ...

verifying the user's screen dimensions

I'm currently exploring ways to customize the appearance of my website based on the viewer's screen resolution. I am interested in potentially optimizing the layout for vertical screens on mobile devices, and horizontal screens for laptops. Addit ...

Utilizing Input Data from One Component in Another - Angular4

As a newcomer to Angular, I'm facing an issue where I need to access a variable from ComponentA in ComponentB. Here's the code snippet that demonstrates what I'm trying to achieve (I want to utilize the "favoriteSeason" input result in the " ...

Avoid repeated element IDs when using .push() in Angular

I am faced with a challenge involving an array of objects named users, defined in my component as users: Array<GroupUser> = [];. My objective is to verify that a new applicant does not have the same id as any existing user in the array. Below is the ...

Why is the Jquery PHP MySQL login not properly returning the correct value after sending data to MySQL?

Issue: It appears that the data is being successfully written to the database, but the $action variable does not get set to 'register' in the insert.php call from the HTML file, resulting in a null PHP JSON return. Can someone provide assistance ...

Is there a way to capture the stdout and stderr output from a WebAssembly module that has been generated using Emscripten in JavaScript?

In my C++ code snippet below: #include <iostream> int main() { std::cout << "Hello World!" << std::endl; return 0; } I compile the code using: emcc -s ENVIRONMENT=shell -s WASM=1 -s MODULARIZE=1 main.cpp -o main.js This c ...

Node.js cannot locate the require function, resulting in a ReferenceError at node:internal/main/run_main_module:23:47

Currently, I am diving into the world of Node.js (Node.js v18.12.1) and embarking on my first server-building journey for a backend application. Below is the snippet of the initial code I have put together: const express = require("express"); const app = ...

There seems to be an issue with the AJAX REST call failing to transmit data

Whenever I attempt to submit the form, the page refreshes and nothing gets saved in the database. The code in subforum.js $(document).on('click','#submit',function(e) { var user = JSON.parse(sessionStorage.getItem("ulogovan")); consol ...