Clicking on the search box will remove the item that is currently displayed

Currently, I am working on creating a dropdown menu that includes a text box. The goal is for the items to appear when the text box is clicked, and for the selected item to turn green once clicked and then display in the text box. I'm interested in finding a solution where clicking on the item in the search box would deselect it (turning it unselected or ungreen). How can I detect the click event on the element name within the text box?

Answer №1

Not sure if this is exactly what you're seeking, but it might assist you in achieving your goal.

<select multiple>
<option class="anOption">One</option>
<option class="anOption">Two</option>
<option class="anOption">Three</option>
<option class="anOption">Four</option>
</select>

Javascript

var options = document.getElementsByClassName("anOption");
for(var i=0; i<options.length; i++){
options[i].addEventListener('click', function (){
this.style.backgroundColor="green";
});
}

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 module 'fs' cannot be resolved by Webpack Express due to an expression dependency in the request

Every time Express is added to my project, I encounter these errors during the webpack build process. webpack.config.dev.js var path = require("path") module.exports = { entry: { "server": "./server/server.ts" }, output: { path: path.resol ...

"Ensuring Data Accuracy: Validating WordPress Fields with JavaScript

Can anyone provide assistance with this? I've been struggling for some time now. I am in need of a JavaScript code that can compare two fields in a contact form to ensure they match. For example, Phone Number and Phone Number Again. Both fields must ...

The data is not displaying properly when using angularjs ng-repeat

My webpage is experiencing an issue that I can't seem to resolve: app.controller('MessageController',['$http',function($http){ http.get("DataManage.php?metodo=GetMessage") .then(function(response){ this.Me ...

How can I ensure that $routeProvider functions correctly within my AngularJS application?

I'm currently in the process of manually constructing a shell and trying to understand its functionality Shell Structure: - application (contains PHP files) - webroot -- app --- app.js -- templates --- main ---- login ----- login.html index.html ...

JSON to Table Conversion

On the hunt for a php script that can load CSV files? Look no further than this snippet: echo json_encode(file(csvfile.csv, FILE_IGNORE_NEW_LINES)); Check out the output: ["foo,bar,baz","foo, foo,bar","bla,bla,blubb"] Need to integrate this into your ...

Showcase a Pair of Files Next to Eachother using HTML

I am a beginner with HTML and I am trying to accomplish a seemingly simple task. I have searched for similar questions and attempted the solutions provided, but none have worked for me. My goal is to have two documents displayed side by side on a web page ...

The Omniture dashboard is displaying an incorrect URL in My Reports

I am currently exploring the possibility of integrating Omniture into my website. I have received some JavaScript code, but I am unsure whether I should include it on the page or simply add additional parameters to the s object properties. When viewing t ...

Trouble arises when working with ko.toJSON and Computed observable

I am struggling with a problem related to knockout computed observable and toJSON functions. I have set up an example on Fiddle Example. In this demo, I have defined a model : function VM() { this.Name = ko.observable("Tom"); this.Age = ko.ob ...

Appium and Selenium working together to easily remove text from a field

Is it possible to select and delete the entire text in an edit field? I am currently automating a mobile app using Selenium Appium with Java, and there is a field containing 10-15 characters. My goal is to clear the existing content in the field and then u ...

Leveraging underscore.js for null verification

let name = "someName"; if(name !== null) { // perform some action } Currently, I am utilizing http://underscorejs.org/#isNull. How can I achieve the same functionality using underscore.js? Is there any noticeable performance enhance ...

Uploading files and data in Laravel using Vue.js and Vuetify: A step-by-step guide

My Vuetify form is working fine with text input, but when I try to include images, I encounter the error GET http://danza.test/thumbnails[object%20File] 404 (Not Found). How can I successfully pass both text and images in a form? This is part of the code ...

Using the tensorflow library with vite

Greetings and apologies for any inconvenience caused by my relatively trivial inquiries. I am currently navigating the introductory stages of delving into front-end development. Presently, I have initiated a hello-world vite app, which came to life throug ...

The border of the Material UI Toggle Button is not appearing

There seems to be an issue with the left border not appearing in the toggle bar below that I created using MuiToggleButton. Any idea what could be causing this? Thank you in advance. view image here view image here Just a note: it works correctly in the ...

Error encountered in a pre-existing project due to Vuetify configurations

Having trouble downloading Vuetify 3 onto my Vue 3 app, I keep encountering an error. Despite attempting to use 'npm install --save @popperjs/core vuetify/styles', it's still not working as expected. I'm curious to understand the root c ...

Generate a dynamic animation showcasing transparency effects, uncovering the intricate Mesh structure within threejs

Is there a way to achieve a linear animation in THREE.JS that reveals a complex 3D mesh from the bottom to the top, similar to a slideUp effect in 2D jQuery objects? After searching for options such as opacity channels or maps, it seems that they are not ...

Can you show me the method to retrieve the value of client.query in Node JS using PG?

I have been working with node.js to establish a database connection with postgresql. Here is what my dbConfig.js file looks like: var pg = require('pg'); var client = new pg.Client({ host:'myhoost', port:'5432', ...

Encountered a 404 error while trying to delete using VueJS, expressJS, and axios. Request failed with

Currently, I'm in the process of learning the fundamentals of creating a MEVN stack application and facing a challenge with the axios DELETE request method. The issue arises when attempting to make a DELETE request using axios, resulting in a Request ...

How can I dynamically insert HTML content into a data-attribute using JavaScript?

I have been trying to insert a variable that includes HTML code into a DATA attribute (a href ... data-content= ...), but it isn't functioning properly. The code I input seems to delete certain characters and as a result, it does not display correctly ...

Determine whether the function name in a given string matches a JavaScript or PHP

I am currently attempting to highlight code and eventually sanitize the HTML, but I am encountering an issue with my regex not matching just the function name and parameters. Regex is not my strong suit and can be quite confusing for me. Additionally, when ...

Node.js C++ addons provide accessors for interacting with Node.js from native

How can I implement setter and getter for a global variable 'X' in a C++ extension for Node.js? I am encountering issues with undefined 'x' while using the getter and setter methods. Currently working on a program involving Accessors ...