Duplicating a dropdown menu using a list of checkboxes (jQuery)

Is there a way to convert a fieldset containing a legend and a list of checkboxes/radio buttons into a select HTML element? It might sound like an unusual request, but unfortunately, it's something I need to accomplish.

I have created a jsfiddle for reference: https://jsfiddle.net/demj49st/

The main issue I am encountering is replicating the click behavior of a select element. I am struggling to find the correct selector that will prevent the fake select box from disappearing when a checkbox is clicked.

(function($) {

    $(document).ready(function() {

        $('fieldset legend').on('click', function() {
            var $this = $(this);
            $this.toggleClass('active');
            $this.next().toggleClass('visible');
        })


        $(document).on('click', function (e) {
            if($('ul').hasClass('visible') && !$('fieldset legend').is(e.target)) {
                $('ul').removeClass('visible');
                $('legend').removeClass('active');
            }
        });

    })

})(jQuery);

Answer №1

    $(document).ready(function() {

        $('section heading').on('click', function() {
            var $thiselement = $(this);
            $thiselement.toggleClass('activated');
            $thiselement.next().toggleClass('visible');
        });


        $(document).on('click', function (e) {
           if (!$("section > ul , section > heading").is(e.target) // if the target of the click isn't the container...
                && $("section > ul , section > heading").has(e.target).length === 0) // ... nor a descendant of the container
            {

                $('section > ul').removeClass('visible');
            }
        });

    });

VIEW DEMO

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

Tips for creating a responsive input field within a navbar

Can anyone assist me with setting the input field width in my navbar to adjust to the remaining width of the navbar and be responsive to different device sizes? I need the input field to dynamically change its width based on the screen size. Any help would ...

What is the easiest method to iterate through nested objects within an object?

Hey there! I'm working on implementing a "your cart" feature for an ecommerce website in my project, and I could use some assistance with coding to loop through an object within an object. Below is the code snippet I've been working on: ...

Comprehending the significance of *this* within class structures

I've got this code snippet below. class Node { constructor(value, parent, possibleChildren = []) { this.value = value; this.parent = parent; this.children = [] this.setChildren(possibleChildren); } setChildren(possibleChil ...

Selenium: To find an element using XPath for a radio button, you cannot use the contains() method with the @id attribute

I'm working on a list of radio buttons with color labels. Currently, I am able to select the radio button labeled Red, which is the third option: driver.find_element_by_xpath("//tr[@id='600,-2147649943:1107:Red||_tr']//input[@name='doc ...

Automatically populate input fields by extracting data from the URL using BootstrapVue

Utilizing BootstrapVue, I am attempting to populate my input fields based on the URL provided. This is my current code: <template> <div class="col-md-6 mt-3"> <div class="mt-2">ID</div> <b-form-inpu ...

Retrieve the data from every dropdown menu

How can I retrieve the selected values from all created selects when a button is clicked? I have attempted using both refs and v-model, but neither of them are functioning as expected. <div v-for="attribute in attributes" class="col"> {{ a ...

Material UI AppBar fixed position that is not part of a grid container

Hey everyone, I'm really struggling with this issue. I recently set my AppBar to have a position of "fixed". However, now the appbar is no longer contained within its container and instead spans the entire screen. I've been trying to figure it ou ...

Trouble with executing Mongoose find within an asynchronous function

Even after referring to this stack overflow solution, I still couldn't resolve my issue. The find method works perfectly when used inside a controller but not within an async function. Here is the cron job where I'm calling this function in my ...

Color of selected Bootstrap radio toggle buttons

Can anyone help me figure out how to customize the colors of radio toggle buttons using my style.css in Bootstrap? I've managed to change the background color in normal mode, but I'm struggling with changing the checked state. I've searched ...

unable to get highcharts to redraw and reflow properly

I am currently working on creating a dynamic page that can display between 1-4 graphs. These graphs need to be able to resize when added or removed from the page. However, I have encountered a major issue with resizing the graphs after resizing the contain ...

Ways to make a chosen row stand out in an *ngFor loop?

Struggling to find a solution within Angular2 for setting a css class when selecting a row. I want to achieve this without relying on jQuery. <table class="table table-bordered table-condensed table-hover"> <thead> <tr> ...

Obtain the Cartesian coordinates of a sphere in THREE.js using SphereGeometry

I am currently working on creating a tile engine that utilizes a SphereGeometry, but I am facing difficulties in extracting the 2D plane of the geometry. This is crucial for me as I intend to optimize network efficiency by focusing only on the visible area ...

"Developing CSS styles that work seamlessly across all

What methods do professional web designers use to develop cross-browser compatible CSS? Is it typically a manual process, or are there specific toolkits available to simplify the task similar to YUI for JavaScript? I am looking to avoid WYSIWYG editors s ...

Exploring the dynamic loading of JavaScript functions with Ajax in Dojo while passing arguments

Currently, I am working on implementing a require/load function that handles the retrieval and execution of remote JavaScript code. The function is functioning well, but I have been using a workaround to pass arguments by setting global variables. However, ...

Ways to extract output from another file's standard output?

Take this scenario for instance, Consider having the following code snippet in a file labeled main.js: function main(){ console.log(5); } Now, imagine another file where you aim to extract the stdout from main.js. Presently, your code looks like t ...

What are the methods for identifying default browsers for LinkedIn and Skype?

In my app, I have a specific section where I want to show different content when the user accesses it through various mobile browsers like LinkedIn browser, Skype browser, and other default mobile browsers. <audio id="audioplayer" playsinline controls l ...

The PHP mail() function seems to be having trouble delivering emails to Hotmail users, although it works fine with all

Utilizing the php mail() function on my dedicated server via a php script, I am able to send emails to all recipients except for those using Hotmail accounts. Below is the code snippet for the mail() function: $hyperlink = 'http://test.guru99.com/&ap ...

Using jQuery to restrict users from entering a character consecutively more than three times within a single word

Is there a way to restrict users from repeating the same character more than three times in a single word? For example, allowing "hiii" but not "hiiii" to be typed in a textbox? I am looking for something similar to how the textbox works on this website f ...

Enhance your website with the jQuery autocomplete feature, complete with

Is there a way to incorporate smaller text descriptions alongside the search results displayed on my website? The descriptions are available in the data array used by autocomplete and can be accessed using the .result function by calling item.description. ...

HeaderView in Angular Framework

When exploring the best practices for organizing an AngularJS structure, I came across the recommendation to implement partial views as directives. Following this advice, I created a directive for my app header. In my specific header design, I included a ...