Can someone assist me in setting a maximum height for the autocomplete list items?

Working on an HTML autocomplete box example, I have it functioning well. I am now seeking guidance on setting a height for the autocomplete items to prevent them from becoming too lengthy when there are numerous items. I would like to establish a max-height so they can be scrolled through.

Click here for an example

Answer №1

To limit the height of the autocomplete feature to a maximum of 100px in your example, you simply need to include max-height: 100px and overflow-y: scroll within the style for the .autocomplete-items class.

Here is a demonstration:

// JavaScript code for autocomplete functionality
function autocomplete(inp, arr) {
    // Function logic goes here...
}

// Array of country names for autocomplete suggestions
var countries = ["Afghanistan", "Albania", ... ];

// Initialize autocomplete function on input element with id "myInput"
autocomplete(document.getElementById("myInput"), countries);
/* CSS styles for autocomplete feature */
.autocomplete {
    /* Styles for container go here... */
}

input[type=submit] {
    /* Button styles */
}

.autocomplete-items {
    /* Styles for autocomplete dropdown */
    max-height: 100px;
    overflow-y: scroll;
}

.autocomplete-active {
    /* Active item styles */
}
<h2>Autocomplete</h2>

<p>Start typing:</p>

<form autocomplete="off" action="/action_page.php">
    <div class="autocomplete" style="width:300px;">
        <input id="myInput" type="text" name="myCountry" placeholder="Country">
    </div>
    <input type="submit">
</form>

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

modifying input field with radio button selection in Jquery

Is there a way to set the input field to have a default value and then change it, or disable it with a different value? $("#tax").value(19).prop("disabled",false); $("#tax").value(0).prop("disabled",true); <script src="https://ajax.googleapis.com/aj ...

Having difficulties with getting D3 JavaScript/JSON to execute

I've been attempting to run this tutorial script, but for some reason, I just can't seem to get it to execute properly. Can someone please take a look and help me figure out what I'm missing? I would greatly appreciate it! Here is the origi ...

Retrieving data from Firebase database using multiple JavaScript calls

Currently, I am utilizing ReactJs for my project to develop a chatting application. Within this project, I have implemented 2 components: ChatRoom.js and InputMessage.js. The purpose of ChatRoom.js is to load chats from the Firebase Realtime Database, wher ...

Using JavaScript's AJAX to create conditional statements with the if-

Below is my JavaScript code that is meant to display a notification to the user if the PHP script below returns a MySQL select count result greater than 0: <script> $(document).ready(function() { function update() { ...

Issues with image uploads in Node.js database storage

Trying to update an image using the put method is resulting in a 'filename' undefined error. Interestingly, the image updates successfully when editing without changing the image. The code snippet below shows the implementation: app.put('/a ...

Develop a strategy for aligning images below text of varying heights within the Bootstrap Grid system

I am currently facing the challenge of vertically aligning images inside a Bootstrap grid below text with containers of varying heights. Click here for the Fiddle Here is the current state: https://i.sstatic.net/Oo4UM.jpg This is my desired outcome: htt ...

Issues with React's useState hook not updating properly

As a React beginner, I am facing an issue with my input field filtering functionality. The problem arises when the results are filtered and rendered, as it seems to be ignoring the last character of the input value. For instance: If I type in "s", nothing ...

What is the process of importing a database into an application?

Could someone please provide a simple explanation of how this function operates? I have also included a snippet of PHP code that I believe is responsible for fetching data from the database. Please correct me if I'm mistaken. Thanks in advance! ...

Items seem to vanish into thin air and become immovable when attempting to relocate them

I am attempting to create a unique grid layout with 3x3 dimensions, where each grid item represents a fragment of a single image. These items should have the capability to be dragged and dropped inside another 3x3 grid, in any desired sequence. I have hit ...

Conflict between multiple jQuery files

Currently, I am in the process of creating a test website for an upcoming project. However, I have encountered some issues with jQuery. This is a static HTML5 website that includes a social widget. The problem arises when I remove a particular jQuery lin ...

Discovering how to create a line break within a text area using the $scope feature in Angular

I'm looking to incorporate a text area for chat input that is resizable. I would like to have some pre-filled texts in it, such as: Hi Jhon, Thanks for contacting us.... I want the text to appear on a new line after the existing content in the textar ...

Unlocking the power of .NET with JavaScript

As a beginner in JavaScript, I am eager to leverage .NET using JavaScript without being restricted to a specific browser. My goal is to be able to execute my code in Internet Explorer, Edge, or Chrome. I have heard about different versions of JavaScript ...

What is the best way to display a JavaScript loop on a webpage instead of just in the console?

As a beginner in programming, I have taken on the challenge of creating my own FizzBuzz program. The goal is to write a program that displays numbers from 1 to 100. However, for multiples of three, it should output "Fizz" instead of the number; for multipl ...

Generate a visual representation in JavaScript/Angular by counting from 0 to 1000

Here are my numbers: https://i.sstatic.net/S31aX.jpg I am trying to count from 0 to 1,000 using images and want to separate them with commas. For instance, when the number is equal to 1, I want to display 1.png. What's the most efficient way to ac ...

What is the best way to seamlessly transition from responsive design to mobile design?

While developing a single-page app, I encountered an issue with the layout when the screen width reaches a specific point. On narrow screens, I prefer to utilize the full width, but for wider screens, I want to limit the width for better readability. The l ...

Pass the extended object from the service to the controller within a promise using the .map function

section: I am currently working on extending a JSON object within a service and passing it to a controller. The JSON originally came to the service from another service that makes backend calls. The code is quite intricate, so I've added comments an ...

Ways to maintain the position of a column when the one before it is hidden?

Is there a way to keep the third column in place when the second column is hidden upon clicking a button? Take a look at my code snippet below: <div class="row"> <div class="col-3"> Column 1 </div> <div class="col-3 test"& ...

Getting the value of a variable within the scope of AngularJS can be achieved by utilizing

I have an ng-repeat directive in my code that displays slides. Here is a snippet of the data: slides = [{ src: "/sikiosk/slidedata/Global/NA/USNS/Cafeteria/5000_24.jpg", interval: 5000 }, { src: "/sikiosk/slidedata/Global/NA/USNS/Cafeteria/5000_login-regi ...

no visible text displayed within an input-label field

Currently, I have started working on a multi-step form that is designed to be very simple and clean. However, I am facing an issue where nothing is being displayed when I click on the next arrow. I am puzzled as to why it's not even displaying the te ...

An element in CSS that has a position of fixed and a width of 100% surpasses the dimensions of its

My element has the CSS properties position:fixed and width:100%, causing it to be larger than its parent elements. Despite the complexity of my code, I have attempted to simplify it for better understanding. Below, you can see that the green box exceeds ...