Stylish Combobox with jQuery and CSS

Seeking assistance in creating a stylish combo box using CSS and jQuery. The combo box will contain 3 items, and I need guidance on how to select items within the combo box and display the selected item. Any help or suggestions would be greatly appreciated! Below is the snippet of my code:

http://jsfiddle.net/vmCBJ/

 $(function () {
        $('#clickIcon').click(function() {

        });
        $("ul.dropdown li").click(function () {
            $(this).addClass("hover");
            $('ul:first', this).css('visibility', 'visible');

        }, function () {

            $(this).removeClass("hover");
            $('ul:first', this).css('visibility', 'hidden');

        });

        $("ul.dropdown li ul li:has(ul)").find("a:first").append(" » ");

    });


<div id="page-wrap">
    <span>
        <ul class="dropdown">
            <li><a href="#">Cannot</a>
                <ul class="sub_menu">
                    <li>
                        <a href="#">Can</a>
                    </li>
                    <li>
                        <a href="#">Must</a>
                    </li>

                </ul>
            </li>

        </ul>
    </span>
    <span id="clickIcon" style="background-color: gray;padding-left: 5px;font-weight: 900;">
        V
    </span>
</div>

Answer №1

Here is the solution I came up with: http://jsfiddle.net/K2ndZ/

I have redesigned it to make the functionality more evident. It is worth noting that this solution may not work if the user's browser does not support javascript. Perhaps using a drop-down menu would be a more reliable option?

Here is the HTML code:

<div class="combobox">
    <div class="selector">Please select</div>
    <ul>
        <li>Option 1</li>
        <li>Option 2</li>
        <li>Option 3</li>
    </ul>
</div>

And the corresponding CSS:

.combobox {cursor:pointer;display:inline-block;}
.combobox .selector{border:1px solid #cccccc;padding: 2px 5px}
.combobox ul {padding:0;margin:0;display:inline-block;border:1px solid #eeeeee;background-color:#cccccc;}
.combobox li {padding: 2px 5px}

Lastly, the jQuery script:

$(document).ready(function(){
    $('.combobox ul').hide();
    $('.combobox').hover(
        function(){
            $(this).find('ul').stop().slideDown();
        },
        function(){
            $(this).find('ul').stop().slideUp();
        }
    );
    $('.combobox li').click(function(){
        $(this).parents('.combobox').find('.selector').text($(this).text());
    });
});

Answer №2

It's important to note that StackOverflow isn't here to write code for you. It's essential that you conduct your own research. Simply indicating which item in the list is "active" won't be sufficient to create a fully functional combobox. In fact, what you've created is more of a custom dropdown than a combobox.

I've made some modifications to your code (which should not be used in a live environment) to guide you in the right direction. You can view the updated code here.

Here's what I changed:

    $(".sub_menu a").click(function () {
        var currentItem = $(this).text();
        $active = $('.dropdown a').first();
        $(this).text($active.text());
        $active.text(currentItem);

        return false;
    });

This code captures the click event in the submenu. It saves the text from the item that was clicked in a variable, locates the "active" element (the one outside of the submenu), and swaps the text between the clicked item and the "active" one.

However, it's worth mentioning that this setup still falls short of a proper combobox implementation :)

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

The presence of <!--[if lte IE8]>< ![endif]--> on the website is causing problems with the dropdown

Check out my link: Problem #1: Why does "" appear on the site in Internet Explorer? The markup seems correct, so what is causing this to show up? Problem #2: In Internet Explorer, there is an issue with clicking on subnav items above the jQuery slider wh ...

Displaying the "Ad Blocker Detected" image next to the advertisement

I am attempting to create a feature where if adblock is enabled, an image will display behind the ad banner on my website with the message "Please consider disabling adblock". Unfortunately, it is only showing up as a bordered box. Here is how the box ap ...

Steps to incorporate Ajax into current pagination system built with PHP and Mysql

Greetings! I am a beginner programmer looking to enhance my PHP & Mysql based pagination with Ajax functionality. Despite searching through numerous tutorials, I have been unsuccessful in finding a guide that explains how to integrate Ajax into existin ...

Leveraging the "dialogclose" event within jQuery Mobile

Is there a way to change an image once it is clicked and change back when the dialog it links to is closed? I found a potential solution on this site, but after modifying it for my needs, it doesn't seem to work. Can anyone help me figure out what I a ...

Conceal and reveal elements using a specific identifier with

Web Development Guide <ul id="menüü"> <li id="meist"> <p><a href="meist.html">Meist</a></p> </li> </ul> <div id="submenu"> <ul id="submeist"> ...

Alignment of label not remaining centered vertically, issue with bootstrap

Struggling with getting this label to stay centered vertically? It always seems to appear at the top. Any help would be greatly appreciated. Thank you! Check out the code here <div class="container"> <div class="row"> <div clas ...

Troubleshooting issues with Bootstrap's responsiveness configuration

I've been working on creating a responsive user login page using Angular 5. It looks great on full-screen display. https://i.sstatic.net/YQrL5.png However, when I resize the browser window, the responsiveness seems to break. https://i.sstatic.net/4 ...

showing the chosen item in a Bootstrap dropdown [limited to the first menu option and using the attr() method]

Is there a way to dynamically display the selected menu option using the bootstrap drop down? Initially, I was able to select the first menu option and apply it to the bootstrap drop-down button. However, I encountered an issue where I cannot change the ...

Using jQuery .sortable() to reorder list items and update their IDs based on their new positions

My apologies for my limited English skills, but here is what I am trying to achieve. I have a list structured like this: <ul id="sortable" class="sortable"> <li id="1">1</li> <li id="2">2</li> <li id="3">3</li> &l ...

An automated feature that smoothly transitions a large image onto the screen

I came across a plugin, possibly a slideshow plugin, that smoothly slides a large image along the y-axis. It's hard to explain, but imagine the visible image is only 600px by 300px, while the actual image is 600px by 600px. This plugin would scroll t ...

Fade between two divs using jQuery works perfectly with images, but unfortunately, it does not work with iframes

Edit: Uploaded live, with images, and with iframes I am experimenting with making pane1 fade into pane2. It successfully works with images, but I am curious if it can be achieved with iframes. Is this feasible? Any insights or suggestions are much appreci ...

Steps to establish the starting value as 'Concealed'

I stumbled upon this interesting example that demonstrates how to create expandable/collapsible text when clicked. My question is, how can I initially set the value to be hidden so that the paragraph starts off collapsed and expands only when clicked? He ...

Ways to increase the values in a textbox

My task involves handling checkboxes that are populated with data from a database. The goal is to have certain checkboxes pre-checked based on the retrieved data, and if any are not checked, their values should be entered into a textbox. For instance, con ...

Tips for maintaining interactivity after a page refresh

$(document).ready(function () { $("#2").click(function () { $("#content").load("{% url 'about' %}"); }); $("#3").click(function () { $("#content").load("{% url ...

Obtain the value for every span class and insert the corresponding string into the input field

I have a series of tags labeled as 'tag-label' in spans. My goal is to combine the values of these tags and insert them into an input field, separating each value with commas. To demonstrate this, I've created a JSFiddle that showcases the ...

Display the delete message URL according to the user's name

I have a wall message system using PHP and jQuery where messages from all users are displayed. Now, I want to add delete functionality to each message, but the delete link should only be visible to the user who posted the message. .childs { opaci ...

Clicking the checkbox in IE9 to activate its function

Strange Behavior of Click Function in Internet Explorer: I have noticed that when you click a checkbox multiple times, it does not always get checked or unchecked as expected. However, the click function still triggers. This issue was observed in IE8 as we ...

Substitute all text within the HTML element

I am trying to update the occurrences of '20' within the id="olderpost" with '30' This is what I attempted: <div id="olderpost"><a onclick="$.ajax({ dataType: 'script', url: '/actions/olderpost/20'}); retu ...

What is the best way to ensure a jQuery UI slider recognizes when the mouse is released after being clicked?

I have integrated the jQuery slider into my application and am facing an issue with triggering an event to update the database upon releasing the slider. Currently, I am using the following code snippet: $('.ui-slider-handle').mouseup(function () ...

Is there a way to position my input at the bottom of its parent element?

Is there a way to position this input element at the bottom and center of its parent div? https://i.sstatic.net/gs1yP.jpg ...