Conceal the value when selecting from the datalist

In my current project, I am utilizing datalist to present a list of choices. The issue I am facing is that both the value and text are being displayed in the dropdown, as expected with datalist. However, I specifically need to hide the value and only show the text.

After extensive research, I came across a solution that involves using data-value instead of just value. Despite implementing this change, I have been unsuccessful in getting it to work as intended. The reason I am sticking with datalist is that I require a functional searchable dropdown for my project. Otherwise, I would have abandoned it altogether.

The code snippet I am working with is as follows:

var myinput = document.getElementById('example');
var divs = document.querySelectorAll('.hidden');
myinput.addEventListener('change', function() {
    var mydiv = document.getElementById(this.value);
    divs.forEach(div => {
      div.style.display = div.id === this.value ? 'block' : 'none';
    });
});
.hidden {
  display: none;
}
<fieldset>
    <legend>This is a datalist</legend>
    <input type="text" id="example" list="brow" />
    <datalist id="brow">
        <option value="div1">Choice 1</option>
        <option value="div2">Choice 2</option>
    </datalist>
</fieldset>

<div class="hidden" id="div1">This is div1</div>
<div class="hidden" id="div2">This is div2</div>

Answer №1

Ensuring the value in your dataList is given top priority, it is important to keep the option value and option text consistent to prevent duplications.

<fieldset>
<legend>Displaying a datalist</legend>
<input type="text" id="example" list="brow" />
<datalist id="brow">
    <option value="Choice 1">Choice 1</option>
    <option value="Choice 2">Choice 2</option>
</datalist>

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

jQuery Animated List - Nested items are unresponsive to clicks

Looking to create a dynamic nested list using jQuery for animations, but unsure of the best approach. Currently, I'm adjusting the length of the parent list item and revealing the nested items. The issue is that the parent item's length covers ...

Embedding complex JSON information into an HTML dropdown menu

Here is a JSON string that I am working with: { "electronics": { "cameras": [ "sony", "nikon", "canon" ], "TV": "none", "laptop": [ "hp", "apple", "sony" ] }, "home": { "furniture": [ ...

Converting javascript html object lowercase

Is there a way to dynamically adjust the height of specific letters in my label? Right now, I am overriding the text for the elements: let element = document.getElementById('xxx') element.textContent = 'Label' I attempted using <sup ...

What is the best way to style an icon in CSS?

I am facing an issue with the following code snippet: <span class="stars">★★☆☆☆ </span> My objective is to style the empty stars differently, but I am unsure how to specifically target them. The following CSS rule does not produc ...

Discovering the corresponding elements within an array of objects

Within my array of objects, I am performing a search let arr = [ { name:"string 1", arrayWithvalue:"1,2", other: "that" }, { name:"string 2", arrayWithvalue:"2", other: "that" }, { name:"string 2", arrayWithvalue:"2,3", other: "that" }, ...

What is the best way to remove the help button on the WordPress dashboard?

Being a beginner in Wordpress, I am trying to figure out how to hide the help button on the top right of the dashboard in the admin backend without resorting to plugins or code removal. My plan is to simply add display:none to a CSS file, but I am having ...

Utilizing Nginx to Reverse Proxy to Node.js and Implement Rewrites

I have various applications running in the background behind an Nginx reverse proxy. One of these applications is a Node server using Express.js. I am forwarding domain.com/demo/app/<path> to localhost:7003/<path> through this Nginx configurati ...

Utilize the v-if directive within a v-for loop

I am currently iterating over an array named cars using the v-for directive. I would like to implement a condition within this loop so that if the color property matches a certain value, a specific CSS style is applied. <p v-for="car in cars" ...

Is there a more optimized CSS approach for this layout?

I attempted to create this layout using only CSS (no JavaScript, etc.) within an existing HTML page (without altering the HTML code). Unfortunately, I couldn't achieve all the positions that I needed. Let's see if anyone has a better idea. Thanks ...

Executing javascript href using Python in Selenium

Currently, I am attempting to use Selenium in Python to click on a href JavaScript link. The HTML code appears as follows: HTML Example and my goal is to click on javascript:goType(1). This is the approach I have taken: advance_search = browser.find_el ...

The reason for setting a variable as void 0 in JavaScript

Currently, I am delving into the libraries referenced in this particular article as well as other sources. There are some truly mind-boggling concepts contained within these resources, one of which is highlighted by the following line: var cb = void 0; I ...

Searching for a specific character sequence within a MongoDB database

I am looking to create a small file system-like structure in MongoDB. Here is an example of what my object could look like: { "\":{ 'autoexec.bat':{ name:'autoexec', filetype:'bat', ...

Turn off pagination for tables in Material-UI

Currently, I am utilizing the TablePagination component from Material-UI in my React project. Unfortunately, this component does not come with a built-in disabled prop. I do have a boolean variable called loading that I would like to utilize as a paramet ...

Ways to decrease the height of the navigation bar in Bootstrap version 4.1

Struggling to figure out how to decrease the height of the navbar in bootstrap v4.1. I want it to be specifically 24px tall; Thanks, Richard. ...

Tips on transferring information from a child component to a parent component

Currently, I am exploring ways to transfer data from a child component to a parent component. After extensive research, I have yet to find a satisfactory solution. If anyone has a solution, could you please explain how to solve this issue? APP.js impor ...

Selection determines the value of two fields

This form has predefined values that are used to create a link, but the issue is that these values vary depending on the location. Can you assist me with this problem? <input type="radio" id="iconapp1" name="department" value="1250"/><label for ...

Passing PHP values to JQuery is a common practice in web

I have a PHP file named userAuthentication.php containing a function that returns either "true" for successful authentication or "false" for a failure. In addition, I have a login view called login.html which includes fields for inputting username and pas ...

The Dragula NPM package appears to be functioning smoothly during development environments, yet encounters issues when transitioning to the

I have developed a sophisticated drag and drop feature using React 15 and Dragula 3.7.2. However, when I bundle my Application for production, the drag and drop functionality ceases to work properly. I am only able to lift up a single element but cannot fi ...

algorithm for selecting the greatest value

I'm attempting to create a function that identifies the largest number in an array, but for some reason, I'm only able to retrieve the first number in the array. function findLargest(numbers) { var bigNum = 1; for(i = 0; i < numbers.len ...

Having trouble loading the .php file with my Ajax request

Attempting to retrieve data from the postcode.php file and display it in a #postcodeList div, but encountering issues as nothing happens. Upon inspecting the postcode.php file, it seems to be outputting all the correct information. var loadicecream = do ...