Creating your own custom selection is an exciting opportunity to personalize your choices

I am interested in creating my own customized select element.

Currently, the design of my select element is as follows:

https://i.sstatic.net/eQNXb.png

JS Fiddle

This is the code for how it is currently implemented:

<p>Title</p>
<form>
    <input type="color" onchange="colorisPanneau(value);pageSuivante();" name="coloris_panneau" list="liste_color3" id="coloris_panneau" value="#C5B9A9" class="formc" style="height:24px;width:202px;">
    <datalist id="liste_color3">
        <option value="#FFFFFF">
        <option value="#999999">
        <option value="#000000">
        <option value="#582810">
    </datalist>
</form>

However, I would like to transform it into something similar to this:

https://i.sstatic.net/OhoJA.png

Is there a way I can achieve this?

What steps should I take to customize the interface of my combobox?

Additionally, where can I find resources or documentation to help me with this customization process?

Thank you for your assistance!

Answer №1

It's impossible to modify the datalist using CSS, even with some CSS magic. You'll never be able to have multiple items inside the datalist options. The only workaround is to create a select box from scratch.

To get you started, here's something I put together for you:

$('html').on("click", function(e) {
  $('.color-picker').removeClass('open');
});
$(document).on("click", ".color-value", function(e) {
  e.stopPropagation();
  if ($('.color-picker').hasClass('open')) {
    $('.color-picker').removeClass('open');
  } else {
    $('.color-picker').addClass('open');
  }
});
$(document).on("click", ".list-item", function() {
  var color = $(this).data('color');
  $('#color-input').val(color);
  $('.color-value').html($(this).html());

  // This now becomes the value of your hidden input field
  $('#value').html(color);
});
* {
  box-sizing: border-box;
}

.color-picker {
  height: 30px;
  width: 150px;
  overflow: hidden;
  color: #666;
  background: #FFF;
}

.open {
  overflow: visible;
}

.list {
  border: 1px solid #CCC;
  border-top: none;
  background: #FFF;
}

.list-item {
  padding: 5px;
  cursor: pointer;
}

.list-item:hover {
  background: #f1f1f1;
}

.list-item > span {
  display: inline-block;
  vertical-align: middle;
  height: 20px;
  line-height: 20px;
}

.list-item > span:first-child {
  width: 20px;
  margin-right: 5px;
}

.color-value {
  height: 30px;
  line-height: 30px;
  padding: 0 5px;
  width: 100%;
  cursor: pointer;
  border: 1px solid #CCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="color-picker">
  <input id="color-input" type="hidden" name="coloris_panneau" value="" />
  <div class="color-value list-item">Select your color</div>
  <div class="list">
    <div class="list-item" data-color="#edae0e">
      <span style="background:#edae0e;"></span>
      <span>Yellow</span>
    </div>
    <div class="list-item" data-color="#ff0000">
      <span style="background:#ff0000;"></span>
      <span>Red</span>
    </div>
    <div class="list-item" data-color="#336699">
      <span style="background:#336699;"></span>
      <span>Blue</span>
    </div>
  </div>
</div>

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

Validation is key! jQuery form validation triggers a popup notification upon successful submission

I am currently working on implementing a popup that appears when the user clicks "Submit" and all inputs are valid. Here is my current progress: $(document).ready(function() { $("#aForm").validate({ debug: false, errorPlacement: functi ...

Upload files using Ajax

Looking to implement Ajax and PHP for file upload functionality. The form includes an <input type="file"> tag, and I aim to enable users to upload a file without having to refresh the page. Any guidance on how to achieve this? While a refresh is no ...

How to initiate a download via a GET request in axios

I am currently utilizing Vue.js 2 with Axios to send a GET request and pass parameters to the server in order to retrieve a PDF as a response. The server is powered by Laravel. Having issues with force downloading the PDF even though the correct headers a ...

Passport verification is successful during the login process, however, it encounters issues during registration

I am currently learning about passport.js and session management, and I'm in the process of implementing a local login feature on my website. Here is what I am attempting to achieve: Secret page: Authenticated users can access the secret page, while ...

Having difficulty with the useState hook in a material ui functional component that integrates with Firebase

Currently, I am endeavoring to create a sign-up form utilizing a Material UI functional component. Within this form, I am aiming to trigger a signup event by using the "onClick" attribute attached to the sign-up button. My approach involves passing the ema ...

.value not being grabbed from options of selected index

I have a form that includes a select field with the id of "to". My goal is to retrieve the selected value within a JavaScript function. Interestingly, the first time I execute this function, it fails to capture the selected value. However, upon running t ...

Having difficulty adjusting the text to match the image alignment

Having trouble aligning the text tag inside the H1 above the image. It keeps appearing in the wrong place, either on the left or right. I've tried including the h1 inside the container, but it just disappears. <!doctype html> <html> <h ...

Steps to troubleshoot a scrollbar problem while applying flexbox in CSS

I'm currently working with a front-end framework that utilizes a flex layout. In my development of an admin page, I have included sections for the sidebar, header, and main. My objective is to ensure that the sidebar and the header + main sections fun ...

The hide and show buttons seem to be missing when utilizing the datatable API

For my current project, I referenced this example: https://datatables.net/extensions/responsive/examples/styling/bootstrap.html Despite trying various datatable API examples, I'm still unable to display the expand/collapse buttons. The required CSS a ...

Tips for effectively handling repetitive bootstrap CSS code

As part of my coding routine, I often utilize the following code snippet to center specific content: <!-- horizontal --> <div class="d-flex align-items-center justify-content-center h-100"> .. </div> <!-- vertical --> & ...

When utilizing the array.push method, new elements are successfully added, however, it appears to also overwrite the last object

I'm encountering a strange issue where every time I push an object to the array, the array's length increases as expected. However, the object that I push last ends up overriding all other objects. I can't seem to figure out my mistake, so p ...

How can I prevent Bootstrap from conflicting with my custom styles?

My HTML menu was functioning perfectly with specific CSS styles, until I decided to add a Bootstrap reference in the header: New Link Added <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> Afte ...

Is there a way to reorganize the image/text grid using a media query?

In my grid, each row contains text in one column and an image in the other. The order of these columns is determined by the user in a CMS and can vary for each row. https://i.sstatic.net/MwR5O.png On mobile devices, I always want the image to appear abov ...

Localhost Firebase authentication with Facebook integration

I am currently working on a Vue.js app that integrates Firebase for authentication, specifically with the Facebook provider. Despite configuring my Firebase code correctly, I continue to encounter the "Can't load URL: The domain of this URL isn't ...

Is it possible to apply a delay function to JQuery's AjaxStart/Stop events?

As I was working on my single-page app, I thought about adding a spinner and gray background to provide feedback to users while processes are loading. I discovered JQuery's AjaxStart() and AjaxStop() functions, which allow me to display the spinner du ...

Combine several POST requests by utilizing AJAX and jQuery

I recently received some advice regarding my code, suggesting that I should chain a series of POST requests together. However, I am unsure of how to accomplish this task. The recommendation was to: fire off a post, have its success handler fire off the ...

Angular Boilerplate is experiencing difficulties in properly reading ABP

Working on my boilerplate project, I am now diving into consuming backend services (using asp .net) in Angular through http requests. However, I encountered an issue when trying to implement the delete method in mycomponent.ts, as TypeScript was not recogn ...

What is the best way to assign the value of an HTTP GET request to a subarray in Angular 8

Attempting to store data in a sub-array (nested array) but despite receiving good response data, the values are not being pushed into the subarray. Instead, an empty array is returned. for (var j=0;j<this.imagesdataarray.length;j++){ this.http.g ...

Text that is arranged vertically and adjusts its size and layout

I'm currently facing a challenge with vertical text display Check out my plunker What I'm attempting to accomplish is vertical text where: If a short text is placed inside the vertical text div, it should be aligned in the middle of the div I ...

Resizing Div elements in React

I am currently working on a Drawer navigation feature and I am exploring the option of enabling mouse drag resize functionality. To achieve this, I have included a div element where I listen for the onMouseDown event. Once triggered, I then add an event li ...