Customizing jQuery-UI's Select-Menu Widget with CSS

I am facing some challenges with setting CSS properties for jQuery-UI widgets, especially the SelectMenu. Basic styling like margins does not seem to work, and I am struggling to assign different CSS styles to two separate SelectMenu widgets. Here is a simplified version of my code:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>JQuery Select Option</title>
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <script>
    $(function () {
      $("#currency_selector").selectmenu();
      $("#currency_selector2").selectmenu();
    });
  </script>

  <style>
    label {
      display: inline-block;
    }
    .ui-widget {
      display: inline-block;
      overflow: hidden;
      color: black;
      background-color: white;
    }
    .ui-selectmenu-open {
      max-height: 180px;
      overflow-y: scroll;
    }
    .fieldSpace {
      margin-top: 10px;
      margin-bottom: 50px;
    }
  </style>
</head>

<body>
  <div>
    <label for="currency_selector">Currency:</label><br>
    <select id="currency_selector" class="fieldSpace"><br>
      <option disabled>Popular Currencies</option>
      <option value="EUR">Euro (€)</option>
      <option value="USD">US Dollar ($)</option>
      <option value="GBP">UK Pound Sterling (£)</option>            
    </select>
    <br><br>
    <label for="currency_selector2">Currency:</label><br>
    <select id="currency_selector2" class="fieldSpace"><br>
      <option disabled>Asian Currencies</option>
      <option value="SGD">Singapore Dollar ($)</option>
      <option value="MYR">Malaysian Rinngit (RM)</option>
      <option value="IDR">Indonesian Rupiah (R)</option>            
    </select>        
  </div>
</body>

</html>

  1. Can I apply different background colors to the two selectors?
  2. Is there a way to hide the scroll bars?
  3. Why isn't it recognizing the fieldSpace class that defines the margins?

Appreciate any help.

Answer №1

To achieve the desired theming, you can utilize the classes option to add custom classes to specific elements.

Here is an example:

$(function() {
  $("#currency_selector").selectmenu({
    classes: {
      "ui-selectmenu-button": "fieldSpace bg-white"
    }
  });
  $("#currency_selector2").selectmenu({
    classes: {
      "ui-selectmenu-button": "fieldSpace bg-blue"
    }
  });
});
.menu-wrap {
  position: relative;
  height: 60px;
}

label {
  display: inline-block;
  width: 100px;
}

span.ui-selectmenu-button {
  display: inline-block;
  position: absolute;
  top: -16px;
  left: 101px;
}

div.ui-selectmenu-menu[class^='bg'].ui-selectmenu-open {
  max-height: 180px;
  overflow-y: scroll;
}

span[id^='currency_selector'].fieldSpace {
  margin-top: 10px;
  margin-bottom: 50px;
}

span.bg-white {
  background: white;
}

span.bg-blue {
  background: #eef;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<div class="menu-wrap">
  <label for="currency_selector">Currency:</label><br>
  <select id="currency_selector" class="fieldSpace"><br>
    <option disabled>Popular Currencies</option>
    <option value="EUR">Euro (€)</option>
    <option value="USD">US Dollar ($)</option>
    <option value="GBP">UK Pound Sterling (£)</option>
  </select>
</div>
<div class="menu-wrap">
  <label for="currency_selector2">Currency:</label><br>
  <select id="currency_selector2" class="fieldSpace"><br>
    <option disabled>Asian Currencies</option>
    <option value="SGD">Singapore Dollar ($)</option>
    <option value="MYR">Malaysian Rinngit (RM)</option>
    <option value="IDR">Indonesian Rupiah (R)</option>
  </select>
</div>

Additionally, use more specific CSS rules to ensure the correct styling properties are applied.

As this widget conceals the select element, you can employ absolute positioning to control its appearance effectively, resembling a pseudo-inline element.

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

Why is a <script> tag placed within a <noscript> tag?

Lately, I've been exploring various websites with unique designs and content by inspecting their source code. One site that caught my attention was Squarespace, which had blocks of <script> tags within a <noscript> tag: <!-- Page is at ...

Issue with displaying the file-field in Django admin after upgrading from Django 2.1 to version 3

Since upgrading from Django 2.1 to 3, the file field in Django admin is now displaying as "loading". https://i.sstatic.net/8JDWu.png An error is appearing in the console. https://i.sstatic.net/RCgwt.png https://i.sstatic.net/78YtG.png Previously, ther ...

What is the best way to access all of a class's properties in JavaScript?

Is there a way to retrieve all the properties of a class using JavaScript? For example, if I have a class like this: .menu { color: black; width: 10px; } How can I extract "color: black; width: 10px;" as a string using JavaScript? Thanks in advance! ...

I am looking to ensure that the ul li a() elements remain hidden until I hover over the h2 tag, at which point they will be displayed

Insert your code here : <table cellpadding="0" cellspacing="0" align="center" class="TblGreen"> <tr> <td> <h2><a href="#" class="AGreen">Sweater</a></h2> <p>The coding business</p> ...

Step-by-step guide to creating a dynamic button that not only changes its value but also

I am trying to implement a translation button on my website that changes its value along with the text. Currently, I have some code in place where the button toggles between divs, but I am struggling to make the button value switch as well. Given my limit ...

Tips for choosing a sibling element on hover using CSS

I'm having trouble figuring out how to make the rect element change color to purple when the text is highlighted. Right now, only the text color is changing. Is there a way to achieve this using just CSS? The goal is for the rect to turn purple on ho ...

Creating interactive comments in Vue 3 using dynamic rendering

Is there a way to properly display a dynamic comment in Vue 3? I attempted using v-html, but it's not working as expected in my scenario. Here is the example: https://i.sstatic.net/ddX39.png <template> <!-- Method 1: not displaying correctl ...

Insert straight lines between elements in an inline list

I am working on building a step progress bar using the HTML code below: .fa-check-circle { color: #5EB4FF; } .fa-circle { color: #CFD7E6; font-size: 14px; } .container { width: 100%; position: absolute; z-index: 1; margin-top: 20px; } . ...

Implementing a delay of X seconds after a click event in JQuery: A step-by-step guide

Is there a way to delay the triggering of a click event after one has been recently triggered? I am facing an issue on my website where users can click multiple times on the "dropdown icon" and cause it to toggle the slide effect multiple times. What I wan ...

Tips for concealing overflow x on a responsive website

Could use some help here. Whenever I switch to responsive mode, the overflow x feature gets enabled and I'm at a loss on how to disable it. I've attempted using @media to disable overflow but unfortunately it's not working as expected. ...

The code seems to be malfunctioning in a separate JS file, but oddly enough, it functions properly when placed within a <script> tag

I am trying to create a loader, but I have encountered an issue where the script works when placed directly in the HTML file, but not when it is in a separate JavaScript file. Here is the script: var loader = document.getElementById("ld"); w ...

Exploring the height and overflow properties of nested div elements

Imagine a scenario where you have a fixed-size container and need all the elements to fit inside. The issue arises when some elements contain lots of text, causing the overflow to be hidden but still stretching beyond the container's height. How can t ...

What is the best way to center align the div container horizontally?

Trying to center the #container but all methods have failed. How can I achieve centering using only CSS? body { text-align:center; } <div id="app"> <div id="container"><div id="container_red" style="position:absolute;left:0"> ...

Listening to changes in a URL using JQuery

Is there a way to detect when the browser URL has been modified? I am facing the following situation: On my webpage, I have an iframe that changes its content and updates the browser's URL through JavaScript when a user interacts with it. However, no ...

Utilizing HTML templates efficiently without the need for a view engine

For a new application, we are implementing multiple servers for handling different functions - one server for delivering HTML files, another as a proxy to handle HTTPS requests, and a Java backend with a database. The view server should be simple, with an ...

CSS: The border line with a see-through section

I'm in the process of creating a div that has a unique design like this. https://i.sstatic.net/cHJNY.png .triangle-area { width: 100%; height: 150px; } .triangle1 { width: 100%; height: 50px; border-width: 50px 50px 0 50px; border-col ...

Gathering information from an HTML form and displaying it through JavaScript by utilizing Bootstrap's card component

I've been working on integrating form data from my index.html page into a card component using Bootstrap. I've successfully collected the data in the backend, but now I'm struggling to display it on the frontend as a card component. Any help ...

Using a snippet of HTML code from one Angular component in another component

Is it possible to use a specific div element from one Angular component in another component? main component.html <html> <div1> some elements </div1> <div2> some elements </div2> In the child ...

`Issue with Firefox's rendering of HTML tables`

I have noticed that the table appears differently in Firefox compared to IE8/Chrome. You can view the website here: I would like the table to look more like it does in IE8/Chrome, where the lines are a light gray color instead of completely black. Any s ...

The Android webview fails to load the page, but the app successfully loads it when accessed through the

I'm experiencing an issue with my HTML5 mobile web app loading fine in a browser but getting stuck on an Android webview. We have implemented a splash screen before loading the web app, but the webview seems to be stuck on the splash screen and does n ...