Tips for entering multiple values in an input field

I am attempting to implement a feature where multiple names can be added from an autocomplete drop-down menu to an input field, similar to the example shown below:

https://i.sstatic.net/D4hGA.jpg

Here is what I aim to create:

  1. Upon selecting an item from the drop-down, it should appear in the input field exactly as depicted in the image.
  2. If I press backspace in the input field, the entire name should be deleted at once.
  3. I want to be able to add and store multiple names within the input field.

In essence, each name should be contained within its own box, which will then be housed within the input field itself. How can I achieve this using JavaScript?

I have searched online for a solution, but I may not be using the correct keywords. I am unsure of what this feature is called.

Answer №1

If you're looking for a solution, one approach is to:

<div>
  <div role="button">Eight</div>
  <input />
</div>

The idea is to have a container where you insert selected options dynamically (using inner divs) and then include an input element to occupy the remaining space. Additionally, consider using a Menu component to manage the list of options.

  1. One way to achieve this is by utilizing JavaScript. Keep track of the clicked value and insert it into the container. You can create a function to extract the label from the options.

  2. Another method involves listening to key events within the input field and handling them accordingly...

  3. It might be more effective (and simpler) to display additional options outside the input.

For a practical example, check out material-ui Autocomplete. You can inspect its implementation via your browser or explore the code on GitHub. Alternatively, explore other libraries that offer similar functionalities.

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

Check out this stylish Twitter-inspired SVG text counter created with a combination of CSS and jQuery! See it in action here: https://jsfiddle.net/moss24

Looking to develop a text counter similar to Twitter that increases the width in green color up to 75%, then switches to yellow until 98%, and finally turns red if the input value is greater than or equal to 400. It should also add a "highlight" class when ...

What is the best way to incorporate the .then method in a promise?

I'm currently working on some JS Express code that looks like this: app.get('/lists2', (req, res) => { mongo.getDB() .then(db => db.collection('dogs')) .then(collection => collection.find().toArray()) .then ...

Ways to streamline a form containing several duplicate rows and make it more user-friendly

Looking for some advice on implementing a "working hours" module. Just need something simple like: Monday: 10:00 - 18:00 ... Saturday: 11:00-16:00 with some additional info. I'm currently attempting to... <form id="working_hours"> <s ...

Can a CSS class be designed to have a value that changes dynamically?

Let's say we have some div elements that look like this: <div class="mystyle-10">Padding 10px</div> <div class="mystyle-20">Padding 20px</div> <div class="mystyle-30">Padding 30px</div> Would it be possible to c ...

Navigating to the default landing page using basic authentication middleware in Express

I'm currently working on implementing basic authorization for an entire website using Express. The goal is to have users enter their credentials, and if correct, they will be directed to the standard landing page. If the credentials are incorrect, the ...

Error message: Selenium is having trouble finding the search bar, regardless of the selector method (such as xpath, css_selector, fullXpath, etc.)

My attempt to automate the search process on Facebook has hit a roadblock. I have tried the following code: search = driver.find_element_by_xpath('//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input') ...

What is the best approach for rendering HTML on a page both initially and dynamically as it is updated, given the heavy utilization of ajax?

One challenge faced by many web applications today is how to efficiently reuse html templates without redundancy when initially rendering a page and adding new content. What is the most effective approach for this? One option is to render the HTML page ...

Unexpected outcomes arising from using nested arrays with jQuery validation plugin

I have been utilizing the jQuery validation plugin from . I am encountering an issue with validating a nested array "tax_percents[]" that is within another array. The validation seems to only work for the first value in the array, and even for the second f ...

Code breaking due to Selenium locating element by class

When my application opens a login page, it looks for valid combinations. Initially, there is a button labeled as "check availability" that Selenium can locate and click on. If the username is already taken, a new button appears with the text "check anothe ...

Sorting through items based on several URL parameters

In an effort to be concise yet clear, I am working on an ecommerce website using Next.js and Shopify. This site features products that need to be filterable based on certain attributes. I retrieve products from the Shopify API, each with its own Product Ty ...

Executing a function in Angular 2 depending on the class assigned to a <div>

In my HTML code, I am using *ngFor to iterate through an array of messages. <div *ngFor="let message of messages; let i=index" [focused]="i === activeIndex;" [ngClass]="{'message-list-active': activeIndex === i }" (click)="onAddtoMessag ...

Is there a different method like Calc() to create a static sidebar alongside content?

Is there a way to achieve a fixed sidebar on the left and a content area on the right without using calc() in CSS? I'm looking for a more universally supported method that works across different browsers. .left-sidebar { width: 160px; ...

Differences between Sequelize classMethods and instanceMethods

Embarking on my Node journey, I've decided to explore Sequelize. Initially, my focus was on understanding the following code: 'use strict'; var crypto = require('crypto'); module.exports = function(sequelize, DataTypes) { var U ...

What is the best way to expand a scrollbar's width with CSS?

Can the width of a scrollbar on a <div> element within the <body> be increased? I mean the scrollbar specifically on the inner <div> element, not the default browser scrollbar since this page is in full screen mode and does not display t ...

Use the inline IF statement to adjust the icon class depending on the Flask variable

Is it feasible to achieve this using the inline if function? Alternatively, I could use JavaScript. While I've come across some similar posts here, the solutions provided were not exactly what I expected or were implemented in PHP. <i class="f ...

The created function in VueJS may sometimes result in an undefined outcome

I recently started a project with VueJs where I encountered an issue while making a GET request using the Axios library. The data was returned as expected, but I faced difficulties calling the loadUsers function inside mounted. Here is my code snippet: ex ...

What is the best way to ensure consistent code placement at the bottom of my Django Template using inheritance?

Lately, I've been working on incorporating a footer into my layout.html file that is inherited by all other pages within my Django project. I am aiming to create a footer that remains fixed at the bottom of every webpage just like how Stack Overflow ...

Permanently removing artwork from the Canvas

I am facing an issue with my canvas drawing function. Whenever I clear the drawings on the background image by clicking a button, the old drawings reappear when I try to draw again. How can I permanently delete the cleared drawings so that I can start fr ...

Is there a way to prevent pop-up windows from appearing when I click the arrow?

Is there a way to display a pop-up window when the green arrow is clicked and then hide it when the arrow is clicked again? I tried using the code below but the pop-up window disappears suddenly. How can I fix this issue using JavaScript only, without jQue ...

Presentation for a div's background image with the use of jQuery

My header contains a large div with text contents and boxes, along with a background image. Now I want to create a slideshow for this div's background. Does anyone know how to make a slideshow for a div's background image? I've searched ex ...