Show a list when a widget is selected and hide the list when the widget

I encountered an issue with a widget that serves as a wrapper for an input and button. The problem arises when trying to display the dropdown on both input focus and button click. While it works fine on button click, the input only functions properly once; after that, it stops working. Even when selecting an option, the functionality remains erratic. The code snippet below outlines the implementation:

  input = $("<input>")
                .appendTo(wrapper)
                .val(value)
                .attr("title", "")
                .autocomplete({
                    delay: 0,
                    minLength: 0,
                    appendTo: wrapper,
                    source: function (request, response) {
                        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");

                        response(select.children("option").map(function () {
                            var text = $(this).text();
                            if (this.value && (!request.term || matcher.test(text)))
                                return {
                                    label: text,
                                    value: text,
                                    option: this
                                };
                        }));

                    },

                .focus(function () {                    
                    $(".ui-combobox").addClass("focus");                    

                    if (wasOpen) {
                        return;
                    }

                    input.autocomplete("search", "");
                })

                .blur(function () {                 
                    $(".ui-combobox").removeClass("focus");
                });

            // creating the anchor button
            $("<button></button>")
                .attr("tabIndex", -1)
                .addClass("br-r")
                .appendTo(wrapper)
                .mousedown(function () {
                    wasOpen = input.autocomplete("widget").is(":visible");
                })
                .click(function () {
                    input.focus();

                    if (wasOpen) {
                        return;
                    }

                    input.autocomplete("search", "");                   
                });
        },

The above excerpt provides insight into the required functionality and the issues encountered in its implementation.

Answer №1

Here is a suggestion for the given input

input = $("<input type='text'>")
                .appendTo(container)
                .val(data)
                .attr("placeholder", "")
                .autocomplete({
                    delay: 100,
                    minLength: 2,
                    appendTo: container,
                    source: function (request, feedback) {
                        var pattern = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");

                        feedback(choices.children("option").map(function () {
                            var textValue = $(this).text();
                            if (this.value && (!request.term || pattern.test(textValue)))
                                return {
                                    label: textValue,
                                    value: textValue,
                                    option: this
                                };
                        }));

                    }})

                .focus(function () {                    
                    $(".ui-combobox").addClass("focus");                    

                    if (isOpen) {
                        return;
                    }

                    input.autocomplete("search", "");
                })

                .blur(function () {                 
                    $(".ui-combobox").removeClass("focus");
                });

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

Alter the color of Material UI Raised Button when it is hovered over

I am trying to change the background color of a raised button on hover in Material UI. I attempted the following, but it did not work. Is there a way to modify the background color in Material UI for a raised button? Example.JS <RaisedButton ...

What is the best way to utilize the <code> tag for showcasing specific computer code on my webpage without causing any issues with the formatting

I'm currently working on designing a technical documentation webpage using HTML and CSS. I've been facing a challenge when it comes to incorporating code snippets without causing my page layout to break. For instance, inserting the following co ...

What is the best way to retrieve the current color of an element that is in the middle of a transition in Chrome / WebKit?

In Chrome, I have implemented a color transition and now I need to be able to retrieve the color at any given point using JavaScript. Right now, I am accessing .style.color in the DOM, but this only gives me the target value. UPDATE: A while back, someone ...

Conceal the heading 4 element when a specific text is searched

I have a search script that filters the text, but I want to hide the h4 element at the top of each ol. How can I achieve this? The issue I'm facing is that while the text gets filtered correctly, the h4 element (which should be hidden) remains visibl ...

retrieve data from a pre-populated object that was generated by a different HTML file

I currently have 2 HTML files (index.html and event.html). Within my index.html, I am importing a JavaScript file: <script type="text/javascript" src="../js/events/createEventOBJ.js"></script> In this JS file, I have an example with a filled ...

Increase the size of the button temporarily on a website

Hey there, I'm working on a clicker project and ran into an issue. When attempting to adjust the size of the button upon clicking, the animation isn't triggering. My goal is to change the size, not the width, of the button. Any guidance would be ...

Deactivate the input text option

There is a situation where I need to render an input text as disabled, and for this purpose, I have employed the following code snippet: $('#id').attr('disabled', 'disabled'); Is there any alternative method to disable an in ...

lack of information available in the Google Charts line graph

sendPostRequest(service, "fetchData", parameters, function(result) { let response = extractObject(result).response; let chartDetails = new google.visualization.DataTable(); let dataArray = []; for(let i = 0; i < response.dataArray.length; i++){ ...

Problems with CSS animations on mobile devices in WordPress

On my website powered by Elementor Pro, I have implemented custom CSS to animate the Shape Divider with a "Brush Wave" style. Below is the code snippet: .elementor-shape-bottom .elementor-shape-top .elementor-shape body { overflow-x:hidden; } @keyframes ...

Real-time chat system using PHP for one-on-one inquiries with server-sent events (not a group chat)

I'm currently working on developing a live chat inquiry form for a website using HTML server-sent events. I'm utilizing this tutorial as a foundation Here is my plan based on the tutorial: On the client side, users are prompted to enter a use ...

Attempting to retrieve dynamically generated input fields and cross-reference them with structured length .json data

In the process of developing code, I've implemented a for loop to dynamically generate HTML input fields based on the length of some data in a .json file. My goal is to use jQuery to compare the text entered in each input field with the corresponding ...

Easiest method for retrieving complete file paths using JavaScript

There has been much discussion around this issue: I am in need of retrieving the full file path through a web page. The scenario involves an application running on the same machine as the browser, where the application initiates a local HTTP server and ope ...

Adjust MapBox GL map to accommodate a group of markers

If I were to utilize the subsequent code for a mapbox map: mapboxgl.accessToken = '<token>'; var map = new mapboxgl.Map({ container: 'map', style: 'mapbox://styles/mapbox/satellite-v9', center: [-96, 37.8], ...

Incrementing numbers with JavaScript/jQuery in a loop

Based on the example, I aim to dynamically insert new input fields with a numbering sequence (1, 2, 3) assigned to them. The number should increment for each additional input added, corresponding to the format name[+incremented number+][]. My current code ...

Order an array in Javascript based on the day of the month

I am trying to create a unique function that changes the order of a string based on the day of the month. For instance, if the input string is "HELLO" and today's date is the 1st of April, the output should be "LHEOL". On the 2nd of April, it should ...

How can one ensure that the "DataTables" Jquery Plugin has been properly initialized?

One of the challenges I face is having multiple DataTables-Objects within a Form. Is there a way to verify if all of them are initialized correctly? This verification is crucial for me as I rely on Ajax for submitting the form. ...

Retrieve targeted offspring from a pair of CSS progenitors

How can I efficiently target all the input[type=text] children from two separate parent classes in CSS? What is the best approach to achieve this with the following CSS code? .secciones * input[type=text] .subsecciones * input[type=text] { border: no ...

"Mastering the art of text animation: A beginner's guide

If you want to see a cool moving text animation, simply head over to this link: . The text will automatically type out and then clear just like the example below: https://i.sstatic.net/dAZW6.png Ever wondered how to create this kind of text animation? A ...

Chosen Dropdown selection - Retrieve/Receive information

One of the challenges I'm facing involves a dropdown list on my web form that contains various dates. My main query is: How can I retrieve data for the selected date from a MySQL database and present it to the user? Additionally, if the chosen date do ...

Unable to invoke the jQuery function within CakePHP3

I am having trouble calling the jQuery function mentioned below in my CakePHP3 MVC project. There are no error messages, but nothing seems to be happening. The code is set up so that jQuery gets included automatically. The function I am trying to call sh ...