Dropdown Placement Based on Button Click

Looking to create an interactive dropdown menu with the Alloy UI Dropdown Component that appears when a user clicks on one of four buttons. The goal is for this dropdown to be positioned to the left of the clicked button.

var toolsDropdown = new Y.Dropdown({
    boundingBox: '#my-div',
    trigger: '.option',
    hideOnClickOutSide: true,
    hideOnEsc: true
 }).render();

The intention is for the Dropdown menu to dynamically appear next to the button clicked, which are displayed in rows of a table-like structure.

<div id="my-div">
    <div id="container-1">
        <button id="options-btn-1" class="option"  type="button">Option one</button>
    </div>
    <div id="container-2">
        <button id="options-btn-2" class="option"  type="button">Option two</button>
    </div>
    <div id="container-3">
        <button id="options-btn-3" class="option"  type="button">Option three</button>
    </div>
    <div id="container-4">
        <button id="options-btn-4" class="option" type="button">Option four</button>
    </div>
</div>

A listener has been set up to detect each button click event

Y.all('button.option-btn').on('click', displayDropdown);

Struggling to get the functionality to work as expected with the Alloy's Dropdown component - any suggestions or insights would be greatly appreciated.

Considering potentially using YUI's Overlay Widget instead

Overlay is a customizable and flexible widget that supports module format layout with header, body, and footer sections.

Answer №1

Give this a shot:

<head>
  <meta charset="utf-8">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
  <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
 <link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.min.css" rel="stylesheet" />
  <script src="http://cdn.alloyui.com/2.0.0pr5/aui/aui-min.js"></script>
  <link href="http://cdn.alloyui.com/2.0.0pr5/aui-css/css/bootstrap.min.css" rel="stylesheet">
<style>
</style>
</head>
<div id="my-div">
    <div id="container-1">
        <button id="options-btn-1" class="option"  type="button">Option one</button>
    </div>
    <div id="container-2">
        <button id="options-btn-2" class="option"  type="button">Option two</button>
    </div>
    <div id="container-3">
        <button id="options-btn-3" class="option"  type="button">Option three</button>
    </div>
    <div id="container-4">
        <button id="options-btn-4" class="option" type="button">Option four</button>
    </div>

    <button class="btn btn-primary" id="primary">Primary</button>

    <div class="aui-dropdown"  id="drp">
        <a class="aui-dropdown-toggle" data-toggle="dropdown" href="#menu1">
            Dropdown
            <b class="aui-caret"></b>
        </a>
        <ul class="aui-dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li class="aui-divider"></li>
            <li><a href="#">Separated link</a></li>
        </ul>
    </div>


</div>
  <script>
  $(document).ready(function(){

    YUI().use('node', 'node-focusmanager', function (Y) {

          var document = Y.one(document),    
              toggler = Y.one('.aui-dropdown-toggle'),
              dropdown = Y.one('.aui-dropdown-menu'),
              buttonAction = Y.one('#primary'),
              dropDownDiv = Y.one('#drp');
              buttonOne=Y.one("#options-btn-1");
               buttonTwo=Y.one("#options-btn-2");


       buttonAction.on('click', function(e) {
         dropDownDiv.setStyle('top','-20px');
         dropDownDiv.setStyle('left','120px');
        dropdown.toggleClass('aui-show');
        e.preventDefault();
     });

     buttonTwo.on('click', function(e) {
       dropDownDiv.setStyle('top','-125px');
       dropDownDiv.setStyle('left','120px');
      //dropdown.toggleClass('aui-show');
      e.preventDefault();
   }); 


     buttonOne.on('click', function(e) {
       dropDownDiv.setStyle('top','-145px');
       dropDownDiv.setStyle('left','120px');
      //dropdown.toggleClass('aui-show');
      e.preventDefault();
   });


          toggler.on('click', function(e) {
            dropdown.toggleClass('aui-show');
            e.preventDefault();
            e.stopPropagation();
          });

          document.on('click', function() {
            dropdown.removeClass('aui-show');
          });

        });  
  });
  </script>

</body>
</html>

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

Checking for the presence of a specific function in a file using unit testing

I am curious if there is a possible way to utilize mocha and chai in order to test for the presence of a specific piece of code, such as a function like this: myfunction(arg1) { ...... } If the code has been implemented, then the test should return t ...

Switch between attributes of two divs using jQuery

I am currently utilizing the hidden attribute to control the visibility of the .block div within each .item. However, my goal is to ensure only one block is visible at a time. How can I modify the code so that if one block is already visible and another i ...

Calculate the duration in seconds using the console

Is it possible to display the time of an action in seconds instead of milliseconds using console.time? Below is my code: console.log('start load cache'); console.time('cache load ok executed in') // loading from mongo console.timeEnd( ...

PHP, CURL, JSON, AJAX, and HTML - unable to fetch the required data

I am faced with the challenge of transferring data from server 2 to server 1, filling up a form, and submitting it without redirecting the user to server 1. Once the form is submitted, I need the PHP script on server 1 to be executed and send back the resu ...

Minimize the need for reflows and repaints to enhance CSS performance

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My ...

Tips for handling notifications that have been read and unread in a React application

In my current project using React JS, I am tasked with creating a notifications component that will display account-related activities. The notifications need to be sorted into read and unread categories. My main question is how should I manage the read a ...

Angular2 - How to track or listen for (click) events on dynamically inserted HTML elements

I'm trying to inject a string with a dynamically retrieved (click) event into an Angular2 template. Since this string is fetched from the back-end after the DOM is loaded, Angular doesn't recognize the injected event. Here's an example of t ...

Steps to verify a form with controllers in AngularJS

Can someone help me figure out how to validate a form in angularjs using controllers? en$scope.saveDetail = function () { if ($scope.register.Name == "") { alert("Pls fill register Name"); return false; } if ($scope.register.A ...

What is the best way to justify Bootstrap 5 navbar items to the right side?

How can you right-align Bootstrap 5 navbar items? In Bootstrap 4, it was ml-auto. However, this does not work for Bootstrap 5. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" dat ...

Using Mongoose schema with a reference to an undefined 'ObjectID' data type

I am currently working on establishing relationships between my schemas, and I have encountered some issues with my solution. Here is how my device schema looks like: var deviceSchema = schema({ name : String, type : String, room: {type: mongo ...

Exploring the Possibilities: Incorporating xlsx Files in Angular 5

Is there a way to read just the first three records from an xlsx file without causing the browser to crash? I need assistance with finding a solution that allows me to achieve this without storing all the data in memory during the parsing process. P.S: I ...

Is there a way to modify the text color within the thumb-label of the Vuetify v-slider component?

Lately, I've been facing some challenges and my objective is to change the color of the thumb label on my v-slider to a custom one that is defined in the component's design. Can anyone provide guidance on how to achieve this? Regards, Joost ...

Overflow issues with flexbox design on mobile devices

Hello, I'm currently working on creating a sliding panel of images. While I have successfully implemented it, I am encountering some browser compatibility issues. The sliding panel functions perfectly on Chrome desktop, however, when viewed on mobil ...

Distributing actions within stores with namespaces (Vuex/Nuxt)

I'm encountering an issue with accessing actions in namespaced stores within a Nuxt SPA. For example, let's consider a store file named "example.js" located in the store directory: import Vuex from "vuex"; const createStore = ...

What is the best way to reference an Angular constant within a Gulp configuration file?

Is it possible to retrieve an Angular constant within a Gulp file? For example: angular.module('app').constant('env', { url: 'http://localhost:1337/' }); What is the method for accessing this constant inside a function ...

Positioning the sDom in jQuery DataTables

Take a look at the image below: I want to adjust the position of the tableTools buttons (Copy, Excel, CSV) so that they appear aligned with the Search Box. I've experimented with different sDom parameters but haven't been successful. This is th ...

What steps can I take to ensure that the full tingle modal, which includes an image, is visible when the

Upon loading the page, I noticed that if the browser width is greater than 540px, the modal displaying an image gets cut off (see figure below). What steps should I take to ensure that the vertical scroll bar appears immediately? https://i.sstatic.net/cJv ...

Encountering a challenge when attempting to upgrade to Angular 9: Issue with transitioning undecorated classes with DI migration

As I transition from Angular 8 to Angular 9, I encounter an error during the step related to transitioning undecorated classes with DI. While attempting to migrate, I faced an issue with undecorated classes that utilize dependency injection. Some project ...

Incorporate information into a React component

I'm currently working on my initial react component and facing a challenge while adding items to the parent element through an external click event. The user needs to select from a list of search results, and I intend for these selections to be incorp ...

My div is not displaying the background image properly after adding the Bootstrap CSS

Strangely enough, Bootstrap seems to be interfering with the background image of a div. The following is the HTML code: <div class="wide"> <div class="col-xs-5 line"> <hr> </div> <div class="col-xs-2 logo"> Log ...